Creating Line Numbers for RichTextBox in C#

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.

  • Load:
    1. private void Form1_Load(object sender, EventArgs e)    
    2. {    
    3.     LineNumberTextBox.Font = richTextBox1.Font;    
    4.     richTextBox1.Select();    
    5.     AddLineNumbers();    
    6. }    
  • Resize
    1. private void Form1_Resize(object sender, EventArgs e)    
    2. {    
    3.     AddLineNumbers();    
    4. }  

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.

  • MouseDown
    1. private void LineNumberTextBox_MouseDown(object sender, MouseEventArgs e)    
    2. {    
    3.     richTextBox1.Select();    
    4.     LineNumberTextBox.DeselectAll();    
    5. }   

Step 8: Debug your Form & check it out.

Complete Code

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

 

Next Recommended Reading How To Call C# If Else In A Single Line