[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
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
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:
Private Const EM_POSFROMCHAR As Integer = &HD6
Declare Auto Function SendMessage Lib "user32" ( _
ByVal hWnd As IntPtr, _
ByVal Msg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As Integer) As Integer
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 Function
Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
Dim MyCoord As Point
MyCoord = GetCaretCoordinates(RichTextBox1)
Me.Text = MyCoord.X & " and " & MyCoord.Y
End Sub
Re: [2005] Getting The Current TEXT Cursor position (X,Y).
How do i convert an integer to a a System.Drawing.Point?
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:
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?