Hi,
I've a huge file displayed on TextBox. The user is allowed to navigate inside of it with cursor.
What I need is to determine and display the position of the cursor, inside the TextBox, as it moves.
Any ideas ?
Thanks,
Joao.
Printable View
Hi,
I've a huge file displayed on TextBox. The user is allowed to navigate inside of it with cursor.
What I need is to determine and display the position of the cursor, inside the TextBox, as it moves.
Any ideas ?
Thanks,
Joao.
u mean line number and column number ?
Just column number.
Joao
You would do something like this, although this will definitely need refinement:When the loop exits the actual column index should be contained in the columnIndex property. I think this code palys up a bit if there are empty lines though. Anyway, you get the general idea, so you can play with it and work out the kinks.VB Code:
Dim columnIndex As Integer = myTextBox.SelectionStart Dim lineIndex As Integer = 0 While columnIndex > myTextBox.Lines(lineIndex).Length columnIndex -= myTextBox.Lines(lineIndex).Length + 2 lineIndex += 1 End While
Thanks, gonna try it.
But which event triggers this action ?
Tried with TextBox39.TextChanged, but it only shows the position if any character is changed within the TextBox.
Is there an event generated when mouse moves inside the TextBox ?
I'd suggest that you put that code in a method and call it on the Click and KeyUp events. That way it will be executed whenever the caret position changes, which can happen if the TextBox is clicked, the user types a character or they use an arrow key. Unfortunately a KeyPress event is not raised when using the arrow keys so you must use KeyUp instead, e.g.VB Code:
Private Sub SetColumnIndex() Dim columnIndex As Integer = Me.TextBox1.SelectionStart Dim lineIndex As Integer = 0 While columnIndex > Me.TextBox1.Lines(lineIndex).Length columnIndex -= Me.TextBox1.Lines(lineIndex).Length + 2 lineIndex += 1 End While Me.Label1.Text = columnIndex.ToString() End Sub Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click Me.SetColumnIndex() End Sub Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp Me.SetColumnIndex() End Sub
Great solution. It works perfectly.
Thanks so much jmcilhinney.
Joao
Cool. Don't forget to resolve your thread from the Thread Tools menu.