Results 1 to 3 of 3

Thread: RichTextBox Caret Location?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Location
    Washington State
    Posts
    3

    RichTextBox Caret Location?

    Hi,

    Well, here's another place where I'm stuck. I have gone through available documentation, samples, and experimented, yet I cannot get a RichTextBox to tell me the location of the cursor in a text or rtf file. It seems like such a simple method for VS,NET to include, that it should be such a simple thing...
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ' This works to display the MOUSE position,
    ' but what I really want is the CARET POSITION!
    Dim newCursorLocStr As String
    Dim cursorLocRTBEditor As System.Drawing.Point = _
    rtbEdit.Cursor.Position()
    newCursorLocStr = cursorLocRTBEditor.X & ":" & _
    cursorLocRTBEditor.Y
    lblCursorPos.Text = newCursorLocStr

    'this is the only reference to the RTB caret I can find!
    'Does not return a value! It proves to me, however,
    'the RTFbox actually does know what a caret is and should
    'be able to deliver coordinates from it easily...
    rtbEdit.ScrollToCaret()

    ' ScrollToCaret() Method Help Reference
    ' The ScrollToCaret() method scrolls contents of control until
    ' caret is within visibile region of control. If caret is
    ' positioned below visible region of control, it
    ' will scroll contents of control until caret is visible
    ' @ bottom of control. If caret is positioned above visible
    ' region of control, method scrolls contents of
    ' control until caret is visible at top of the control.
    ' Has no effect if control does not have focus or if
    ' caret is already positioned in visible region of control...
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    What, do I have to write a routine that keeps track of the cursor from scratch? Am I missing something here?

    Thanks, Dobe

  2. #2
    New Member
    Join Date
    Sep 2002
    Posts
    2

    RTB Caret Position

    The only decent way to do it, is to use the API

    Paste the following code into a class:

    Private Const EM_POSFROMCHAR As Integer = &HD6
    <DllImport("USER32.DLL", EntryPoint:="SendMessageA", SetLastError:=True, _
    CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SendMessage(ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    End Function


    Now make a local function that can get the coordinates.

    Private Function GetCaretCoordinates(ByVal aRichTextBox As RichTextBox) As Point
    Dim caret As Integer
    Dim pos As Integer
    Dim clientPos As Point

    caret = aRichTextBox.SelectionStart + aRichTextBox.SelectionLength

    If (caret < 0) Then
    Return Point.Empty
    End If


    pos = SendMessage(aRichTextBox.Handle.ToInt32, EM_POSFROMCHAR, caret, 0)

    If (pos < 0) Then
    pos = SendMessage(aRichTextBox.Handle.ToInt32, EM_POSFROMCHAR, caret - 1, 0)
    End If

    ' pos is really a short of the x and a short of the y,packed into the int:
    ' x is the lower 2 bytes, y is the upper 2 bytes.

    clientPos = New Point(pos And &HFFFF, pos \ &HFFFF)

    Return clientPos

    End FunctionNow make a local function that can get the coordiantes: PAste the following code into a class:

  3. #3
    New Member
    Join Date
    Sep 2002
    Posts
    2

    Usage of the above GetCaretPos

    Usage of the GetCaretCoordinates function above:

    When your calling it from in code...


    Dim MyCoord as Point

    MyCoord = GetCaretCoordinates(richtextbox1)

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