Welcome to Dream.In.Code
Become a VB Expert!

Join 137,230 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,869 people online right now. Registration is fast and FREE... Join Now!




DrawRichTextBoxLineNumbers Problem

 
Reply to this topicStart new topic

DrawRichTextBoxLineNumbers Problem

brottmayer
20 Feb, 2007 - 07:18 AM
Post #1

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
I have created this code. As it's a picturebox function, how do I make it change the size of the drwn numbers to match that of the text in the richtextbox? Thanks.

CODE

    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
        With RichTextBox
            Dim font_height As Single
            font_height = .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(2)).Y _
                  - .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(1)).Y
            If font_height = 0 Then Exit Sub

            'Get the first line index and location
            Dim first_index As Integer
            Dim first_line As Integer
            Dim first_line_y As Integer
            first_index = .GetCharIndexFromPosition(New _
                  Point(0, g.VisibleClipBounds.Y + font_height / 3))
            first_line = .GetLineFromCharIndex(first_index)
            first_line_y = .GetPositionFromCharIndex(first_index).Y

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

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

    Private Sub r_VScroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox.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
        RichTextBox.Text = vbCrLf & vbCrLf & vbCrLf
    End Sub


I tired using this coding to see if it'd work, but it does not.

CODE

    Private Sub RichTextBox_FontChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox.FontChanged
        MyPictureBox.Invalidate()
    End Sub


If anyone can help me, it would be great. Thanks. ques.gif

This post has been edited by brottmayer: 20 Feb, 2007 - 07:19 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 03:16PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month