I would like to include the line numbers on the left side of a multiline textbox, much like what Notepad++ has. It should scroll along with the rest. Is this possible?
Printable View
I would like to include the line numbers on the left side of a multiline textbox, much like what Notepad++ has. It should scroll along with the rest. Is this possible?
Hi oxnume,
Welcome to the Forum :wave:
Are are you currently adding the data to the TextBox?
Ignore my question since you uploaded the image :)
I think your best bet may be to use a DataGridView. The first colum can be line numbers, and a second colum caould hold the entered text.
Possibly:
hmmm...
make the gridlines invisible, first column non-editable and the second column editable. That should make the basic rig. Then you would need to handle certain keys. e.g. when backspace/left key is pressed when cursor is at first character on current row, you should go to the previous row etc.
The other way would be to use a RichTextBox
It's not a simple task in richtextbox either. That's because there is no direct method to add numbered bullets; you should restort to workarounds.
Here's a sample. http://www.codeproject.com/KB/miscct...TextBoxEx.aspx
For what the OP needs, what you suggested would be a nice and clean solution.
That seems like an interesting workaround. However, I would like to be able to multiple lines of text into the box, so I guess that might not work so well...
I made this sample for you.
It uses a Panel, ListBox and TextBox to simulate the functionality. It only shows the basic functionality and may need a few tweaks to streamline it.
You may wrap up the entire thing into a user control so that it doesn't look cluttered and is also reusable wherever needed.
Wow, thank you! With the left side shaded a bit, it looks exactly like what I need!
There is a problem however, when the line overflows the line number will get messed up. Is there a way to check whether a line overflows that will I can make the left side skip a line.
Set the TextBox1.WordWrap = False either in textbox properties (design-time) or in Form_Load code.
Handled mouse click too.
This is the new modified code:
Code:Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
ListBox1.SelectedIndex = TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart)
If ListBox1.Items.Count > Panel1.Height \ ListBox1.ItemHeight Then
Panel1.VerticalScroll.Value = ListBox1.ItemHeight * ListBox1.SelectedIndex
End If
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
SyncLineNumbers()
Dim newHeight As Integer = ListBox1.ItemHeight * ListBox1.Items.Count
If newHeight > Panel1.Height Then
ListBox1.Height = newHeight
TextBox1.Height = newHeight
End If
ListBox1.SelectedIndex = TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart)
End Sub
Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
ListBox1.SelectedIndex = TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart)
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.WordWrap = False
TextBox1.Text = "Your text goes here..."
End Sub
Private Sub SyncLineNumbers()
If TextBox1.Lines.Count <> ListBox1.Items.Count Then
Do While TextBox1.Lines.Count > ListBox1.Items.Count
ListBox1.Items.Add((ListBox1.Items.Count + 1).ToString)
Loop
Do While TextBox1.Lines.Count < ListBox1.Items.Count
ListBox1.Items.RemoveAt(ListBox1.Items.Count - 1)
Loop
End If
End Sub