|
-
Aug 30th, 2005, 06:33 AM
#1
Thread Starter
Member
Determine position of cursor in TextBox ??
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.
-
Aug 30th, 2005, 06:38 AM
#2
Re: Determine position of cursor in TextBox ??
u mean line number and column number ?
-
Aug 30th, 2005, 06:40 AM
#3
Thread Starter
Member
Re: Determine position of cursor in TextBox ??
-
Aug 30th, 2005, 09:48 AM
#4
Re: Determine position of cursor in TextBox ??
You would do something like this, although this will definitely need refinement:
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
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.
-
Aug 30th, 2005, 04:38 PM
#5
Thread Starter
Member
Re: Determine position of cursor in TextBox ??
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 ?
-
Aug 30th, 2005, 05:39 PM
#6
Re: Determine position of cursor in 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
-
Aug 30th, 2005, 06:39 PM
#7
Thread Starter
Member
Re: Determine position of cursor in TextBox ?? [RESOLVED]
Great solution. It works perfectly.
Thanks so much jmcilhinney.
Joao
-
Aug 30th, 2005, 06:45 PM
#8
Re: Determine position of cursor in TextBox ??
Cool. Don't forget to resolve your thread from the Thread Tools menu.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|