code

I am using AddLineNumbers() function to call anywhere in the program.

I am also using the getWidth() function that returns the specific width according to number of lines for auto resizing the line numbers width.

Here's code of AddLineNumbers() function:

  1. public void AddLineNumbers()
  2. {
  3. // create & set Point pt to (0,0)
  4. Point pt = new Point(0, 0);
  5. // get First Index & First Line from richTextBox1
  6. int First_Index = richTextBox1.GetCharIndexFromPosition(pt);
  7. int First_Line = richTextBox1.GetLineFromCharIndex(First_Index);
  8. // set X & Y coordinates of Point pt to ClientRectangle Width & Height respectively
  9. pt.X = ClientRectangle.Width;
  10. pt.Y = ClientRectangle.Height;
  11. // get Last Index & Last Line from richTextBox1
  12. int Last_Index = richTextBox1.GetCharIndexFromPosition(pt);
  13. int Last_Line = richTextBox1.GetLineFromCharIndex(Last_Index);
  14. // set Center alignment to LineNumberTextBox
  15. LineNumberTextBox.SelectionAlignment = HorizontalAlignment.Center;
  16. // set LineNumberTextBox text to null & width to getWidth() function value
  17. LineNumberTextBox.Text = "";
  18. LineNumberTextBox.Width = getWidth();
  19. // now add each line number to LineNumberTextBox upto last line
  20. for (int i = First_Line; i <= Last_Line + 2; i++)
  21. {
  22. LineNumberTextBox.Text += i + 1 + "\n";
  23. }
  24. }
Procedure

Step 1: Start Visual Studio & create new Windows Forms Application in C#.

Step 2: Drag & Drop RichTextBox onto Form1 & Set following properties to it.

Name LineNumberTextBox
BackColor White
BorderStyle None
Cursor PanNE
Dock Left
ForeColor Black
ReadOnly true
ScrollBars None

You can set BackColor & ForeColor to LineNumberTextBox as you wish.

Here you can also set Enabled property to false, but you can not see ForeColor & BackColor.

For ForeColor & BackColor we are set as above properties to LineNumberTextBox.

Step 3:
Drag & Drop RichTextBox onto Form1 & Set Dock property to Fill. T he default name could be richTextBox1.

Step 4: Add above AddLineNumbers() function to your Form1 code or see Complete Code next.

Step 5: Add following events & code to Form1.

Step 6: Add following events & code to richTextBox1.

  1. SelectionChanged
    1. private void richTextBox1_SelectionChanged(object sender, EventArgs e)
    2. {
    3. Point pt = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
    4. if (pt.X == 1)
    5. {
    6. AddLineNumbers();
    7. }
    8. }
  2. VScroll
    1. private void richTextBox1_VScroll(object sender, EventArgs e)
    2. {
    3. LineNumberTextBox.Text = "";
    4. AddLineNumbers();
    5. LineNumberTextBox.Invalidate();
    6. }
  3. TextChanged
    1. private void richTextBox1_TextChanged(object sender, EventArgs e)
    2. {
    3. if (richTextBox1.Text == "")
    4. {
    5. AddLineNumbers();
    6. }
    7. }
  4. FontChanged
    1. private void richTextBox1_FontChanged(object sender, EventArgs e)
    2. {
    3. LineNumberTextBox.Font = richTextBox1.Font;
    4. richTextBox1.Select();
    5. AddLineNumbers();
    6. }

Step 7: Add following events & code to LineNumberTextBox.

Step 8: Debug your Form & check it out.

Complete Code

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace LineNumbers_CSharp
  5. {
  6. public partial class Form1 : Form
  7. {
  8. public Form1()
  9. {
  10. InitializeComponent();
  11. }
  12. public int getWidth()
  13. {
  14. int w = 25;
  15. // get total lines of richTextBox1
  16. int line = richTextBox1.Lines.Length;
  17. if (line <= 99)
  18. {
  19. w = 20 + (int)richTextBox1.Font.Size;
  20. }
  21. else if (line <= 999)
  22. {
  23. w = 30 + (int)richTextBox1.Font.Size;
  24. }
  25. else
  26. {
  27. w = 50 + (int)richTextBox1.Font.Size;
  28. }
  29. return w;
  30. }
  31. public void AddLineNumbers()
  32. {
  33. // create & set Point pt to (0,0)
  34. Point pt = new Point(0, 0);
  35. // get First Index & First Line from richTextBox1
  36. int First_Index = richTextBox1.GetCharIndexFromPosition(pt);
  37. int First_Line = richTextBox1.GetLineFromCharIndex(First_Index);
  38. // set X & Y coordinates of Point pt to ClientRectangle Width & Height respectively
  39. pt.X = ClientRectangle.Width;
  40. pt.Y = ClientRectangle.Height;
  41. // get Last Index & Last Line from richTextBox1
  42. int Last_Index = richTextBox1.GetCharIndexFromPosition(pt);
  43. int Last_Line = richTextBox1.GetLineFromCharIndex(Last_Index);
  44. // set Center alignment to LineNumberTextBox
  45. LineNumberTextBox.SelectionAlignment = HorizontalAlignment.Center;
  46. // set LineNumberTextBox text to null & width to getWidth() function value
  47. LineNumberTextBox.Text = "";
  48. LineNumberTextBox.Width = getWidth();
  49. // now add each line number to LineNumberTextBox upto last line
  50. for (int i = First_Line; i <= Last_Line + 2; i++)
  51. {
  52. LineNumberTextBox.Text += i + 1 + "\n";
  53. }
  54. }
  55. private void Form1_Load(object sender, EventArgs e)
  56. {
  57. LineNumberTextBox.Font = richTextBox1.Font;
  58. richTextBox1.Select();
  59. AddLineNumbers();
  60. }
  61. private void richTextBox1_SelectionChanged(object sender, EventArgs e)
  62. {
  63. Point pt = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
  64. if (pt.X == 1)
  65. {
  66. AddLineNumbers();
  67. }
  68. }
  69. private void richTextBox1_VScroll(object sender, EventArgs e)
  70. {
  71. LineNumberTextBox.Text = "";
  72. AddLineNumbers();
  73. LineNumberTextBox.Invalidate();
  74. }
  75. private void richTextBox1_TextChanged(object sender, EventArgs e)
  76. {
  77. if (richTextBox1.Text == "")
  78. {
  79. AddLineNumbers();
  80. }
  81. }
  82. private void richTextBox1_FontChanged(object sender, EventArgs e)
  83. {
  84. LineNumberTextBox.Font = richTextBox1.Font;
  85. richTextBox1.Select();
  86. AddLineNumbers();
  87. }
  88. private void LineNumberTextBox_MouseDown(object sender, MouseEventArgs e)
  89. {
  90. richTextBox1.Select();
  91. LineNumberTextBox.DeselectAll();
  92. }
  93. private void Form1_Resize(object sender, EventArgs e)
  94. {
  95. AddLineNumbers();
  96. }
  97. }
  98. }