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:
- public void AddLineNumbers()
- {
-
- 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);
-
- LineNumberTextBox.SelectionAlignment = HorizontalAlignment.Center;
-
- LineNumberTextBox.Text = "";
- LineNumberTextBox.Width = getWidth();
-
- for (int i = First_Line; i <= Last_Line + 2; i++)
- {
- LineNumberTextBox.Text += i + 1 + "\n";
- }
- }
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:
- private void Form1_Load(object sender, EventArgs e)
- {
- LineNumberTextBox.Font = richTextBox1.Font;
- richTextBox1.Select();
- AddLineNumbers();
- }
- Resize
- private void Form1_Resize(object sender, EventArgs e)
- {
- AddLineNumbers();
- }
Step 6: Add following events & code to richTextBox1.
- SelectionChanged
- private void richTextBox1_SelectionChanged(object sender, EventArgs e)
- {
- Point pt = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
- if (pt.X == 1)
- {
- AddLineNumbers();
- }
- }
- VScroll
- private void richTextBox1_VScroll(object sender, EventArgs e)
- {
- LineNumberTextBox.Text = "";
- AddLineNumbers();
- LineNumberTextBox.Invalidate();
- }
- TextChanged
- private void richTextBox1_TextChanged(object sender, EventArgs e)
- {
- if (richTextBox1.Text == "")
- {
- AddLineNumbers();
- }
- }
- FontChanged
- private void richTextBox1_FontChanged(object sender, EventArgs e)
- {
- LineNumberTextBox.Font = richTextBox1.Font;
- richTextBox1.Select();
- AddLineNumbers();
- }
Step 7: Add following events & code to LineNumberTextBox.
- MouseDown
- private void LineNumberTextBox_MouseDown(object sender, MouseEventArgs e)
- {
- richTextBox1.Select();
- LineNumberTextBox.DeselectAll();
- }
Step 8: Debug your Form & check it out.
Complete Code