[Webinar] Streamline your web hosting managementRegister Today


Getting text position under mouse cursor?

Posted on 2004-11-02
Medium Priority
Last Modified: 2012-08-14
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?
Question by:xyzzer
LVL 27

Expert Comment

ID: 12474391
"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.
LVL 86

Accepted Solution

Mike Tomlinson earned 1000 total points
ID: 12474514
To get the Insert position for the TextBox from the X, Y mouse coordinates you have to use several SendMessage API calls.

If you change the TextBox to a RichTextBox though, you can get it for free.  The caret will move as you drag over the RTB.  Try it with the small app below.  Drag the Label over the RTB and drop it anywhere in the Text.


Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(272, 24)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "Label1"
        Me.RichTextBox1.AllowDrop = True
        Me.RichTextBox1.Location = New System.Drawing.Point(8, 40)
        Me.RichTextBox1.Name = "RichTextBox1"
        Me.RichTextBox1.Size = New System.Drawing.Size(272, 216)
        Me.RichTextBox1.TabIndex = 3
        Me.RichTextBox1.Text = "RichTextBox1"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(288, 266)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub Label1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
        If e.Button = MouseButtons.Left Then
            Label1.DoDragDrop(Label1.Text, DragDropEffects.Copy Or DragDropEffects.Move)
        End If
    End Sub

    Private Sub RichTextBox1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            e.Effect = DragDropEffects.Copy
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub RichTextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragDrop
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            RichTextBox1.SelectedText = e.Data.GetData(DataFormats.Text)
        End If
    End Sub

End Class

Author Comment

ID: 12480346
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?
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12480454
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.  ;)

LVL 96

Expert Comment

by:Bob Learned
ID: 12482736
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.


Author Comment

ID: 12493517
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.
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12493617
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.


Expert Comment

ID: 13363894

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.


Author Comment

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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question