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.
~IM
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'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
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon
'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.RichT
<System.Diagnostics.Debugg
Me.Label1 = New System.Windows.Forms.Label
Me.RichTextBox1 = New System.Windows.Forms.RichT
Me.SuspendLayout()
'
'Label1
'
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"
'
'RichTextBox1
'
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"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(288, 266)
Me.Controls.Add(Me.RichTex
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Label1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.Mouse
If e.Button = MouseButtons.Left Then
Label1.DoDragDrop(Label1.T
End If
End Sub
Private Sub RichTextBox1_DragEnter(ByV
If (e.Data.GetDataPresent(Dat
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub RichTextBox1_DragDrop(ByVa
If (e.Data.GetDataPresent(Dat
RichTextBox1.SelectedText = e.Data.GetData(DataFormats
End If
End Sub
End Class
Main Topics
Browse All Topics





by: planoczPosted on 2004-11-02 at 08:45:47ID: 12474391
"In short words: how to get the text position at a given point?"....
EventArgs) Handles TextBox1.MouseMove
here is the x, y point of a textbox.
Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.Mouse
Dim x, y As Integer
x = sender.mousePosition.x()
y = sender.mousePosition.y()
End Sub
Still working on the drag drop part.