Click here to Skip to main content
15,892,059 members
Articles / Multimedia / GDI+

Line Numbering of RichTextBox in .NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.07/5 (17 votes)
26 Jan 2007CPOL2 min read 132.4K   2.6K   52  
Line numbering of a RichTextBox control using PictureBox control painting.
Public Class Form1

   Private Sub DrawRichTextBoxLineNumbers(ByRef g As Graphics)
      'calculate font heigth as the difference in Y coordinate between line 2 and line 1
      'note that the RichTextBox text must have at least two lines. So the initial Text property of the RichTextBox should not be an empty string. It could be something like vbcrlf & vbcrlf & vbcrlf 
      Dim font_height As Single = MyRichTextBox.GetPositionFromCharIndex(MyRichTextBox.GetFirstCharIndexFromLine(2)).Y - MyRichTextBox.GetPositionFromCharIndex(MyRichTextBox.GetFirstCharIndexFromLine(1)).Y
      If font_height = 0 Then Exit Sub

      'Get the first line index and location
      Dim firstIndex As Integer = MyRichTextBox.GetCharIndexFromPosition(New Point(0, g.VisibleClipBounds.Y + font_height / 3))
      Dim firstLine As Integer = MyRichTextBox.GetLineFromCharIndex(firstIndex)
      Dim firstLineY As Integer = MyRichTextBox.GetPositionFromCharIndex(firstIndex).Y

      'Print on the PictureBox the visible line numbers of the RichTextBox
      g.Clear(Control.DefaultBackColor)
      Dim i As Integer = firstLine
      Dim y As Single
      Do While y < g.VisibleClipBounds.Y + g.VisibleClipBounds.Height
         y = firstLineY + 2 + font_height * (i - firstLine - 1)
         g.DrawString((i).ToString, MyRichTextBox.Font, Brushes.DarkBlue, MyPictureBox.Width - g.MeasureString((i).ToString, MyRichTextBox.Font).Width, y)
         i += 1
      Loop
      'Debug.WriteLine("Finished: " & firstLine + 1 & " " & i - 1)
   End Sub

   Private Sub r_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyRichTextBox.Resize
      MyPictureBox.Invalidate()
   End Sub

   Private Sub r_VScroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyRichTextBox.VScroll
      MyPictureBox.Invalidate()
   End Sub

   Private Sub p_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyPictureBox.Paint
      DrawRichTextBoxLineNumbers(e.Graphics)
   End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      MyRichTextBox.Text = vbCrLf & vbCrLf & vbCrLf
   End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions