How to Create Line Number

This article demonstrates how to add line numbers to a rich text box control. This is done by adding a picture box control on the left side of the rich text box control. When the appropriate events happen on the Form or on the RichTextBox control we call a method that paints the line numbers on the PictureBox control. This approach is more accurate and efficient than other approaches I’ve seen where another textbox control was used for printing the line numbers. The PictureBox will only print the line numbers that are currently visible in the RichTextBox control.
During the process of constructing this code sample, I initially based it on the code sample provided in the article http://www.codeproject.com/cs/miscctrl/numberedtextbox.asp and enhanced it by doing the following:

  1. Replace the Label control for numbering with a PictureBox
  2. I fixed the scrolling problem with that implementation due to the fact that RichTextBox uses smooth scrolling (scrolling with the scrollbar scrolls the text in pixels, not in lines), as a result sometimes the first line may be displayed in half, at which case the line numbering should address it.

Here is the code for printing the line numbers. Note that we have a RichTextBox control
called MyRichTextBox and a PictureBox control called MyPictureBox. The method gets one
argument which is the Graphics of the PictureBox control.
This is the method DrawRichTextBoxLineNumbers:

  1. Private Sub DrawRichTextBoxLineNumbers(ByRef g As Graphics)
  2.  
  3. 'Calculate font heigth as the difference in Y coordinate
  4.  
  5. 'between line 2 and line 1
  6.  
  7. 'Note that the RichTextBox text must have at least two lines.
  8.  
  9. '  So the initial Text property of the RichTextBox
  10.  
  11. '  should not be an empty string. It could be something
  12.  
  13. '  like vbcrlf & vbcrlf & vbcrlf
  14.  
  15. With MyRichTextBox
  16.  
  17. Dim font_height As Single
  18.  
  19. font_height = .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(2)).Y _
  20.  
  21. - .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(1)).Y
  22.  
  23. If font_height = 0 Then Exit Sub
  24.  
  25. 'Get the first line index and location
  26.  
  27. Dim first_index As Integer
  28.  
  29. Dim first_line As Integer
  30.  
  31. Dim first_line_y As Integer
  32.  
  33. first_index = .GetCharIndexFromPosition(New _
  34.  
  35. Point(0, g.VisibleClipBounds.Y + font_height / 3))
  36.  
  37. first_line = .GetLineFromCharIndex(first_index)
  38.  
  39. first_line_y = .GetPositionFromCharIndex(first_index).Y
  40.  
  41. 'Print on the PictureBox the visible line numbers of the RichTextBox
  42.  
  43. g.Clear(Control.DefaultBackColor)
  44.  
  45. Dim i As Integer = first_line
  46.  
  47. Dim y As Single
  48.  
  49. Do While y < g.VisibleClipBounds.Y + g.VisibleClipBounds.Height
  50.  
  51. y = first_line_y + 2 + font_height * (i - first_line - 1)
  52.  
  53. g.DrawString((i).ToString, .Font, Brushes.DarkBlue, MyPictureBox.Width _
  54.  
  55. - g.MeasureString((i).ToString, .Font).Width, y)
  56.  
  57. i += 1
  58.  
  59. Loop
  60.  
  61. 'Debug.WriteLine("Finished: " & firstLine + 1 & " " & i – 1)
  62.  
  63. End With
  64.  
  65. End Sub
  66.  
  67. Private Sub r_Resize(ByVal sender As Object, ByVal e As System.EventArgs) _Handles MyRichTextBox.Resize
  68. DrawRichTextBoxLineNumbers(Me.MyPictureBox.CreateGraphics)
  69. MyPictureBox.Invalidate()
  70.  
  71. End Sub
  72.  
  73. Private Sub r_VScroll(ByVal sender As Object, ByVal e As System.EventArgs) _
  74. Handles MyRichTextBox.VScroll
  75. DrawRichTextBoxLineNumbers(Me.MyPictureBox.CreateGraphics)
  76. MyPictureBox.Invalidate()
  77. End Sub
  78.  
  79. Private Sub p_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
  80. Handles MyPictureBox.Paint
  81. DrawRichTextBoxLineNumbers(e.Graphics)
  82. End Sub
  83.  
  84. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  85.  
  86. Handles MyBase.Load
  87.  
  88. MyRichTextBox.Text = vbCrLf & vbCrLf & vbCrLf
  89.  
  90. End Sub

Source: Line Numbering of RichTextBox in .NET 2.0

  1. No comments yet.
(will not be published)
Submit Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Subscribe to comments feed
  1. No trackbacks yet.

SetPageWidth