Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms textbox that has line numbers?

Tags:

c#

.net

winforms

I'm looking for a free winforms component for an application I'm writing. I basicly need a textbox that contains line numbers in a side column. Being able to tabulate data within it would be a major plus too.

Does anyone know of a premade component that could do this?

like image 219
FlySwat Avatar asked Sep 24 '08 01:09

FlySwat


People also ask

How do you make a TextBox Autosuggested in Windows Forms?

Append displays first value of the suggestion appended or selected in the TextBox, other values can be navigated using arrow keys. SuggestAppend displays suggested values as dropdown and first value appended in the TextBox. AutoCompleteSource property sets the source for auto complete data.

How do I draw a line in Windows form?

How to Draw Lines Onto a Windows Form Canvas. You can use a Color, Pen, and the DrawLine() method to draw lines on a canvas. Inside the Form1_Paint() function, create a Color object with the color you want the line to be. Then, create a Pen object to draw the line with.

How TextBox works in a Windows Form application?

WinForms TextBox controls are used to get inputs from the user and to display the inputs. TextBox control is generally used for editing text, but it can also be set to read-only. TextBoxes are used to display multiple lines of wrap text to the size of the control.


1 Answers

Referencing Wayne's post, here is the relevant code. It is using GDI to draw line numbers next to the text box.

Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call
    SetStyle(ControlStyles.UserPaint, True)
    SetStyle(ControlStyles.AllPaintingInWmPaint, True)
    SetStyle(ControlStyles.DoubleBuffer, True)
    SetStyle(ControlStyles.ResizeRedraw, True)
End Sub

Private Sub RichTextBox1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.SelectionChanged
    FindLine()
    Invalidate()
End Sub

Private Sub FindLine()
    Dim intChar As Integer

    intChar = RichTextBox1.GetCharIndexFromPosition(New Point(0, 0))
    intLine = RichTextBox1.GetLineFromCharIndex(intChar)
End Sub

Private Sub DrawLines(ByVal g As Graphics, ByVal intLine As Integer)
    Dim intCounter As Integer, intY As Integer

    g.Clear(Color.Black)

    intCounter = intLine + 1
    intY = 2
    Do
        g.DrawString(intCounter.ToString(), Font, Brushes.White, 3, intY)
        intCounter += 1

        intY += Font.Height + 1
        If intY > ClientRectangle.Height - 15 Then Exit Do
    Loop
End Sub

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    DrawLines(e.Graphics, intLine)
End Sub

Private Sub RichTextBox1_VScroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.VScroll
    FindLine()
    Invalidate()
End Sub

Private Sub RichTextBox1_UserScroll() Handles RichTextBox1.UserScroll
    FindLine()
    Invalidate()
End Sub

The RichTextBox is overridden like this:

Public Class UserControl1
Inherits System.Windows.Forms.RichTextBox

Public Event UserScroll()

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    If m.Msg = &H115 Then
        RaiseEvent UserScroll()
    End If

    MyBase.WndProc(m)
End Sub
End Class

(Code by divil on the xtremedotnettalk.com forum.)

like image 105
ine Avatar answered Sep 19 '22 11:09

ine