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

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




Line Numbering Matching Font Size

 
Reply to this topicStart new topic

Line Numbering Matching Font Size

brottmayer
16 Feb, 2007 - 06:15 AM
Post #1

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
Hello,

I've created this function to allow it to count the number of lines that is in the richtextbox but if i change the size of the font, it remains the original size. How could i fix this coding to make it flexible to the changes of the fonts within the richtextbox?

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


Thanks.
User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Line Numbering Matching Font Size
17 Feb, 2007 - 10:40 PM
Post #2

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(brottmayer @ 16 Feb, 2007 - 07:15 AM) *

Hello,

I've created this function to allow it to count the number of lines that is in the richtextbox but if i change the size of the font, it remains the original size. How could i fix this coding to make it flexible to the changes of the fonts within the richtextbox?

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


Thanks.


I have been working on it, and I figured maybe if I entered in this code:

CODE

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


Well, to my disappointment, it didn't work. What am I doing wrong and how can I correct this problem. Thanks.

This post has been edited by brottmayer: 17 Feb, 2007 - 10:41 PM
User is offlineProfile CardPM
+Quote Post

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

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