title: c# richtextbox line numbers column
body: <p>So I have a Windows Form in C# with only 2 rich text boxes. </p>
  
  <p>The main box I use as the text editor and the other box on the left as a line number column. Everything is working fine. I would like to know how to make the line number column rich text box, to not printing the line number if word wrap is enabled and start printing the line number again if enter key is pressed. This should work similar to Notepad++.</p>
  
  <pre><code>1 line 1
  2 line 2
  3 line 3
  4 line 4 - This is a word wrap line that carries on and on. This is a 
    word wrap line that carries on and on. word wrap line that carries on
    and on. This is a word wrap line that carries on and on.
  5 line 5
  6 line 6
  </code></pre>
  
  <p>Here is my code I'm using</p>
  
  <pre><code>  public Form1()
      {
          InitializeComponent();
      }
  
      public void AddLineNumbers()
      {
          richTextBox1.Select();
          Point pt = new Point(0, 0);
          int First_Index = richTextBox1.GetCharIndexFromPosition(pt);
          int First_Line = richTextBox1.GetLineFromCharIndex(First_Index);
          pt.X = ClientRectangle.Width;
          pt.Y = ClientRectangle.Height;
          int Last_Index = richTextBox1.GetCharIndexFromPosition(pt);
          int Last_Line = richTextBox1.GetLineFromCharIndex(Last_Index);
          richTextBox2.SelectionAlignment = HorizontalAlignment.Center;
          richTextBox2.Text = "";
          for (int i = First_Line; i &lt;= Last_Line + 1; i++)
          {
              richTextBox2.Text += i + 1 + "\n";
          }
      }
  
      private void Form1_Load(object sender, EventArgs e)
      {
          AddLineNumbers();
      }
  
      private void richTextBox1_SelectionChanged(object sender, EventArgs e)
      {
          Point pt = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
          if (pt.X == 1)
          {
              AddLineNumbers();
          }
      }
  
      private void richTextBox1_VScroll(object sender, EventArgs e)
      {
          AddLineNumbers();
      }
  </code></pre>