Richt Text Box with numbered lines

May 18 2008 5:31 AM
Hi

A few days ago I started developing a little HTML Editor.
I tryed integrating line numbers into it but im not realy lucky right now.

I use the KeyDown event to respond on the Return/Backspace keys then I count the lines and write it into a second RT-box left to the main Editor RT-box but there are some problems with this solution.
Sometimes there are too much or not enough numbers... then I cant find an event for automatic line breaks.
How would you solve this?

my solution:
  1. private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
  2.         {
  3.             if (e.KeyCode == Keys.Enter)
  4.             {
  5.                 generateTextLineContext(richTextBox1.Lines.Length);
  6.             }
  7.  
  8.             if (e.KeyCode == Keys.Back)
  9.             {
  10.                 generateTextLineContext(richTextBox1.Lines.Length);
  11.             }
  12.         }
  13.  
  14.         private void generateTextLineContext(int lineNumber)
  15.         {
  16.             richTextBox2.ResetText();
  17.             for (int i = 1; i <= lineNumber; i++)
  18.             {
  19.                 richTextBox2.Text += i.ToString() + System.Environment.NewLine;
  20.             }  
  21.         }

Thanks!