Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Getting The Current TEXT Cursor position (X,Y).

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    35

    Resolved [RESOLVED] [2005] Getting The Current TEXT Cursor position (X,Y).

    I am trying to get the TEXT Cursor position (X,Y). the TEXT Cursor , not the actual Cursor . in a RichTextBox on the RichTextBox1_MouseClick.

    I looked everywhere and all i found is the mouse position.
    I was the CARET POSITION
    Last edited by vbcooler; Oct 3rd, 2007 at 08:26 PM.

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

    Re: [2005] Getting The Current TEXT Cursor position (X,Y).

    Obviously you didn't look everywhere. There is no one method or property to give you that value so you have to think a little bit laterally.

    http://www.vbforums.com/showthread.php?t=417602
    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

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    35

    Re: [2005] Getting The Current TEXT Cursor position (X,Y).

    I am looking for the actuale coordiantes (x,y) in relation to the form of the text caret

    I actually found something:

    VB Code:
    1. Private Const EM_POSFROMCHAR As Integer = &HD6
    2.     Declare Auto Function SendMessage Lib "user32" ( _
    3.     ByVal hWnd As IntPtr, _
    4.     ByVal Msg As Integer, _
    5.     ByVal wParam As Integer, _
    6.     ByVal lParam As Integer) As Integer
    7.  
    8.     Private Function GetCaretCoordinates(ByVal aRichTextBox As RichTextBox) As Point
    9.         Dim caret As Integer
    10.         Dim pos As Integer
    11.         Dim clientPos As Point
    12.         caret = aRichTextBox.SelectionStart + aRichTextBox.SelectionLength
    13.         If (caret < 0) Then
    14.             Return Point.Empty
    15.         End If
    16.         pos = SendMessage(aRichTextBox.Handle.ToInt32, EM_POSFROMCHAR, caret, 0)
    17.         If (pos < 0) Then
    18.             pos = SendMessage(aRichTextBox.Handle.ToInt32, EM_POSFROMCHAR, caret - 1, 0)
    19.         End If
    20.         ' pos is really a short of the x and a short of the y,packed into the int:
    21.         ' x is the lower 2 bytes, y is the upper 2 bytes.
    22.         clientPos = New Point(pos And &HFFFF, pos \ &HFFFF)
    23.         Return clientPos
    24.     End Function
    25.  
    26.     Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
    27.         Dim MyCoord As Point
    28.  
    29.         MyCoord = GetCaretCoordinates(RichTextBox1)
    30.         Me.Text = MyCoord.X & " and " & MyCoord.Y
    31.  
    32.     End Sub

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    35

    Re: [2005] Getting The Current TEXT Cursor position (X,Y).

    How do i convert an integer to a a System.Drawing.Point?

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

    Re: [2005] Getting The Current TEXT Cursor position (X,Y).

    Firstly, you don't convert an Integer to a Point, because a Point contains two Integers: one for the X coordinate and one for the Y coordinate. You create a Point from two Integers, e.g.
    vb.net Code:
    1. Dim pt As New Point(10, 20)
    That said, your question is even easier than I thought. You know the character index of the caret from the SelectionStart property. You can get the actual location of that using the RichTextBox.GetPositionFromCharIndex method. That will give you the location relative to the top, left of the RTB. If you want it relative to the parent form you can use the PointToScreen method of the RTB and the PointToClient method of the form. I don't think you looked everywhere at all. Did you read the documentation for the RichTextBox class?
    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