TextBox showing Line numbers

3 posts, 1 answers
  1. Andreas Lohr
    Andreas Lohr avatar
    29 posts
    Member since:
    Aug 2009

    Posted 29 Aug 2010 Link to this post

    hi,
    I would need a TextBox (like RadTextBox), that can show line numbers (similar like in VS).
    Best regards
    Andreas

    P.S.: If that is already possible, please let me know !!
  2. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 01 Sep 2010 Link to this post

    Hello Andreas,

    Thank you for your question.

    RadTextBox does not support adding line numbers. Actually, it internally uses the standard Microsoft TextBox for the textbox part and since the MS textbox does not support this feature, we do not support it as well. If we receive more requests about this feature, we may consider implementing it in one of our future releases.

    Let me know if you have any other questions.

    Best wishes,
    Martin Vasilev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 06 Sep 2010 Link to this post

    Hi, 

    Even though it doesn't come with it, you can use a mix of a picture box and a standard rich textbox to do this. 
    I can't attach a solution here, but follow these steps. 

    1: New Windows Forms Project. 
    2: Drag on a picture box and dock it to the left
    3: Drag on a Rich Text Box and Dock Fill
    4: Add the following code to the code behind: 

    #Region "TextBox_Resize"
        ''' <summary>
        ''' TextBox_Resize
        ''' </summary>
        ''' <param name="sender">The sender</param>
        ''' <param name="e">The event arguments</param>
        ''' <remarks>Invalidates the lines control</remarks>
        Private Sub TextBox_Resize(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles RichTextBox1.Resize
     
            PictureBoxLineNumbers.Invalidate()
        End Sub
    #End Region
     
    #Region "TextBox1_VScroll"
        ''' <summary>
        ''' TextBox_VScroll
        ''' </summary>
        ''' <param name="sender">The sender</param>
        ''' <param name="e">The event arguments</param>
        ''' <remarks>Invalidates the lines control</remarks>
        Private Sub SqlRichTextBox_VScroll(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles RichTextBox1.VScroll
     
            PictureBoxLineNumbers.Invalidate()
        End Sub
    #End Region
     
     
    #Region "PictureBoxLines_Paint"
        ''' <summary>
        ''' PictureBoxLines_Paint
        ''' </summary>
        ''' <param name="sender">The sender</param>
        ''' <param name="e">The event arguments</param>
        ''' <remarks>On Pain, draws the line umbers</remarks>
        Private Sub PictureBoxLines_Paint(ByVal sender As System.Object, _
            ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBoxLineNumbers.Paint
     
            DrawRichTextBoxLineNumbers(e.Graphics)
        End Sub
    #End Region
     
    #Region "DrawRichTextBoxLineNumbers"
        ''' <summary>
        ''' DrawRichTextBoxLineNumbers
        ''' </summary>
        ''' <param name="g">The graphics parameter</param>
        ''' <remarks>Draws teh line numbers on the picture box</remarks>
        Private Sub DrawRichTextBoxLineNumbers(ByVal g As System.Drawing.Graphics)
            If True Then
                Dim font_height As Single = 0
     
                font_height = _
                RichTextBox1.GetPositionFromCharIndex(RichTextBox1.GetFirstCharIndexFromLine(2)).Y - _
                RichTextBox1.GetPositionFromCharIndex(RichTextBox1.GetFirstCharIndexFromLine(1)).Y
                If font_height = 0 Then
                    font_height = 13
                End If
     
                'Get the first line index and location
                Dim first_index As Integer = 0
                Dim first_line As Integer = 0
                Dim first_line_y As Integer = 0
                first_index = RichTextBox1.GetCharIndexFromPosition(New Point(0, Convert.ToInt32(g.VisibleClipBounds.Y + font_height / 3, CultureInfo.InvariantCulture)))
                first_line = RichTextBox1.GetLineFromCharIndex(first_index)
                first_line_y = RichTextBox1.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 = 0
                While y < g.VisibleClipBounds.Y + g.VisibleClipBounds.Height
                    y = first_line_y + 2 + font_height * (i - first_line - 1)
                    Dim s2 As Single = PictureBoxLineNumbers.Width - g.MeasureString((i).ToString(CultureInfo.InvariantCulture), RichTextBox1.Font).Width
                    If i > 0 Then
                        g.DrawString(i.ToString(CultureInfo.InvariantCulture), RichTextBox1.Font, Brushes.Black, s2, y)
                    End If
                    i += 1
                End While
            End If
        End Sub
    #End Region
Back to Top