Link to home
Start Free TrialLog in
Avatar of xyzzer
xyzzer

asked on

Getting text position under mouse cursor?

Hi.
I'm trying to make a TextBox handle Drag & Drop operations and everything is fine and smooth except for the visual feedback I'm trying to achieve.
I want to be able to drag a text data to insert it into the text at the location pointed by mouse cursor.
I can send a mouseclick (MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP) message to the TextBox during the DragDrop event to set the caret position, but doing it before the actual drop causes unwanted DragDrop event to raise and makes things complicated.
Any way to set the caret position right without raising the DragDrop event?

In short words: how to get the text position at a given point?
Avatar of Howard Cantrell
Howard Cantrell
Flag of United States of America image

"In short words: how to get the text position at a given point?"....

here is the x, y point of a textbox.

   Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
        Dim x, y As Integer
        x = sender.mousePosition.x()
        y = sender.mousePosition.y()

    End Sub


Still working on the drag drop part.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xyzzer
xyzzer

ASKER

Thanks a lot! I started with a RichTextBox, but it seemed harder to control, so I got back to regular one. The problem was, that I was dragging a list of strings from the listbox and casted it to string on drop and the RichTextBox only gives feedback to dragged string objects.

By the way, what about:

"To get the Insert position for the TextBox from the X, Y mouse coordinates you have to use several SendMessage API calls."

You do know a way for that?
I'll see if I can whip it up for you.  I could do it in VB6 code but have to convert the calls and what not...tis a pain.  ;)

~IM
If you are trying to drag and drop text into a RichTextBox, then you need RichTextBox.GetCharIndexFromPosition to determine the SelectionStart position for the mouse cursor.

Bob
Avatar of xyzzer

ASKER

I have accepted the answer that worked for me, but if you know how to do it for TextBox - it would be nice too. If got the code for VB6 - it should be enough too.
Thenks again.
I haven't had time to get back to the original TextBox problem.

You have to use the SendMessage() API with EM_CHARFROMPOS to get the row and EM_LINEINDEX to get the column.

~IM
xyzzer,

In .NET, it is simple to find the cursor (insertion point) location in a standard textbox.
Simply query the .SelectionStart property. Even if no text is actually selected, this property tells you at what location in the textbox.text string the cursor currently resides.

ralan2
Avatar of xyzzer

ASKER

That's for keyboard cursor. I needed that for a mouse cursor.