Results 1 to 7 of 7

Thread: How to retreive cursor location from textbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    7

    Question How to retreive cursor location from textbox

    Hi
    Try to create terminal emutalor for VMS.

    Need to get cursor to follow the text in a text box and retreive cursor row and column.

    thanks

    Doug

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: How to retreive cursor location from textbox

    Text box or RichTextBox?
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    7

    Re: How to retreive cursor location from textbox

    I would prefer the text box solution.
    Because I an most familiar with it.


    Doug

  4. #4
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: How to retreive cursor location from textbox

    Doug:
    Cursor column is one thing, but cursor row depends entirely on how wide the text box is vs how large the font is, etc, etc.

    The property Text1.SelStart can give the character location in the textbox, if that helps...?

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    7

    Re: How to retreive cursor location from textbox

    If I had a page of text1.text 80 characters wide by 24 rows high.

    How would I position the cursor at line/row 15 and character position 10?

    thanks

    Doug

  6. #6
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: How to retreive cursor location from textbox

    This function will return the position in pixels, not as row and column, so you might have to convert it somehow.
    VB Code:
    1. Private Type POINTAPI
    2.     X As Long
    3.     Y As Long
    4. End Type
    5. Private Declare Function GetCaretPos Lib "user32.dll" (ByRef lpPoint As POINTAPI) As Long
    6.  
    7. Private Sub Text1_Change()
    8. Dim pt As POINTAPI
    9.  
    10. GetCaretPos pt
    11.  
    12. Me.Caption = pt.X & ", " & pt.Y
    13. End Sub

  7. #7
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: How to retreive cursor location from textbox

    This might be closer to what you need
    VB Code:
    1. Private Const EM_LINEFROMCHAR As Long = &HC9
    2. Private Const EM_LINEINDEX As Long = &HBB
    3.  
    4. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
    5. Private Sub Text1_Change()
    6. Dim Xpos As Long
    7. Dim Ypos As Long
    8.  
    9.     Xpos = Text1.SelStart - SendMessage(Text1.hwnd, EM_LINEINDEX, -1, 0&) + 1
    10.     Ypos = SendMessage(Text1.hwnd, EM_LINEFROMCHAR, -1, 0&) + 1
    11.     Label1.Caption = Xpos & ", " & Ypos
    12. End Sub

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