Results 1 to 8 of 8

Thread: Determine position of cursor in TextBox ??

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    52

    Unhappy 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.

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: Determine position of cursor in TextBox ??

    u mean line number and column number ?

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    52

    Re: Determine position of cursor in TextBox ??

    Just column number.

    Joao

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Determine position of cursor in TextBox ??

    You would do something like this, although this will definitely need refinement:
    VB Code:
    1. Dim columnIndex As Integer = myTextBox.SelectionStart
    2. Dim lineIndex As Integer = 0
    3.  
    4. While columnIndex > myTextBox.Lines(lineIndex).Length
    5.     columnIndex -= myTextBox.Lines(lineIndex).Length + 2
    6.     lineIndex += 1
    7. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    52

    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 ?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Sub SetColumnIndex()
    2.         Dim columnIndex As Integer = Me.TextBox1.SelectionStart
    3.         Dim lineIndex As Integer = 0
    4.  
    5.         While columnIndex > Me.TextBox1.Lines(lineIndex).Length
    6.             columnIndex -= Me.TextBox1.Lines(lineIndex).Length + 2
    7.             lineIndex += 1
    8.         End While
    9.  
    10.         Me.Label1.Text = columnIndex.ToString()
    11.     End Sub
    12.  
    13.     Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
    14.         Me.SetColumnIndex()
    15.     End Sub
    16.  
    17.     Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
    18.         Me.SetColumnIndex()
    19.     End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    52

    Thumbs up Re: Determine position of cursor in TextBox ?? [RESOLVED]

    Great solution. It works perfectly.
    Thanks so much jmcilhinney.

    Joao

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Determine position of cursor in TextBox ??

    Cool. Don't forget to resolve your thread from the Thread Tools menu.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width