Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleTrack the caret's row and column in a TextBox
KeywordsTextBox, track, caret
CategoriesControls
 
In the KeyDown, KeyUp, MouseDown, and Mouseup event handlers, call routine CheckPosition. This routine uses SendMessage to send the TextBox the EM_GETSEL message to get the caret's position. Send the message EM_LINEFROMCHAR to get the caret's line number and EM_LINEINDEX to get the number of characters before the caret's line. Use these values to calculate the row and column.
 
Private Sub CheckPosition()
Dim char_pos As Long
Dim row As Long
Dim col As Long

    char_pos = SendMessage(Text1.hwnd, EM_GETSEL, 0, 0)
    char_pos = char_pos \ &H10000

    row = SendMessage(Text1.hwnd, EM_LINEFROMCHAR, _
        char_pos, 0) + 1
    col = char_pos - SendMessage(Text1.hwnd, EM_LINEINDEX, _
        -1, 0) + 1

    lblPosition.Caption = "(" & Format$(row) & ", " & _
        Format$(col) & ")"
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As _
    Integer)
    CheckPosition
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As _
    Integer)
    CheckPosition
End Sub

Private Sub Text1_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    CheckPosition
End Sub

Private Sub Text1_MouseUp(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    CheckPosition
End Sub
 
Formatted by Neil Crosby
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated