Hi, for the life of me, I am unable to get my program to drag/drop to a textbox. Elsewhere in my program i am dragging to a panel just fine however I always get the "drag not allowed" mouse symbol when attempting to drag to my text box. I HAVE set the AllowDrop Property of the text box to True and have verified that the DragEnter event is fired and detects the proper data.
Here is my code:
Private Const EM_CHARFROMPOS As Int32 = &HD7
Private Structure POINTAPI
Public X As Integer
Public Y As Integer
End Structure
Private Declare Function SendMessageLong Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As _
Int32, ByVal wParam As Int32, ByVal lParam As Int32) As _
Long
' Return the character position under the mouse.
Public Function TextBoxCursorPos(ByVal txt As TextBox, ByVal X As Single, ByVal Y As Single) As Long
' Convert screen coordinates into control coordinates.
Dim pt As Point = txt.PointToClient(New Point(X, Y))
' Get the character number
TextBoxCursorPos = SendMessageLong(txt.Handle, EM_CHARFROMPOS, 0&, CLng(pt.X + pt.Y * &H10000)) And &HFFFF&
End Function
Private Sub Txt_FormatString_DragEnter(sender As Object, e As DragEventArgs) Handles Txt_FormatString.DragEnter
If e.Data.GetDataPresent(DataFormats.StringFormat) Then
' Allow the drop.
e.Effect = DragDropEffects.Copy
Trace.WriteLine("I can drag")
Else
' Do not allow the drop.
e.Effect = DragDropEffects.None
End If
End Sub
' If string data is available, allow a copy.
Private Sub Txt_FormatString_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Txt_FormatString.DragOver
If e.Data.GetDataPresent(DataFormats.StringFormat) Then
' Optionally move the cursor position so
' the user can see where the drop would happen.
Txt_FormatString.Select(TextBoxCursorPos(Txt_FormatString, e.X, e.Y), 0)
End If
End Sub
' Drop the text into the TextBox.
Private Sub Txt_FormatString_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Txt_FormatString.DragDrop
Txt_FormatString.SelectedText = e.Data.GetData(DataFormats.StringFormat)
End Sub
Private Sub Lst_DataTypes_ItemDrag(sender As Object, e As ItemDragEventArgs) Handles Lst_DataTypes.ItemDrag
Dim myItem As ListViewItem
Dim addString As String
' Loop though the SelectedItems collection for the source.
For Each myItem In sender.SelectedItems
addString += GetDeliniator() + myItem.SubItems(0).Text
Next
' Create a DataObject containg the array of ListViewItems.
sender.DoDragDrop(New DataObject(DataFormats.StringFormat, addString), DragDropEffects.Move)
End Sub
I have no clue why I am not allowed to drag to this text box. Any information is greatly appreciated. Thanks!