Advertisement
| 03.01.2008 at 10:46AM PST, ID: 23206426 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: |
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
|