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!

Add this code

Private Sub TextBox1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter

    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        e.Effect = DragDropEffects.All
    End If

End Sub

Private Sub TextBox1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop

    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        Dim files() As String
        files = e.Data.GetData(DataFormats.FileDrop)
        TextBox1.Text = My.Computer.FileSystem.ReadAllText(files(0))
    End If

End Sub

Then try dropping a file on the textbox.

By the way, I don't know if this is a bug, or just a problem with my installation, but I wanted to implement drag and drop to a rich text box, however, the RichTextBox control did not have an AllowDrop property, or DragEnter or DragDrop events available in the IDE. I had to set AllowDrop manually in the form load and add the handlers manually at the same time using AddHandler.

I just thought I'd mention this is case you ever decide to use a RichTextBox instead of TextBox.

Ok, I added the code you suggested and successfully dragged a file to it; the data did appear in the text box. I don't understand what the issue could be here. I made sure that everything detected the proper data format, it just refuses to allow the drag.

So are you saying that now it does work (successfully dragged a file to it) or that it doesn't (it just refuses to allow the drag)?

The code you gave works which is for dragging a file. I am trying to drag text from a list view control (I generated the string I want to appear in the text box) to the text box. Basically, your code only with DataFormats.StringFormat. When I attempt to do this, it does not allow me to make the drop.

I can play around to see what I can come up with but it will have to wait until I finish slogging through my taxes. I should get a chance either later today or tomorrow morning.

This example implements drag and drop from both ListBox and ListView to a TextBox. The ListView is in Details view.

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    For Each word As String In Split("the quick brown fox jumped over the lazy dog")
        ListBox1.Items.Add(word)
        ListView1.Items.Add(New ListViewItem(word))
    Next

End Sub

Private Sub TextBox1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter

    If e.Data.GetDataPresent(DataFormats.StringFormat) Then
        e.Effect = DragDropEffects.Copy
    End If

End Sub

Private Sub TextBox1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop

    'If empty then add text, else start a new line and add text

    If e.Data.GetDataPresent(DataFormats.StringFormat) Then
        TextBox1.Text &= IIf(TextBox1.Text = "", "", vbCrLf)
        TextBox1.Text &= e.Data.GetData(DataFormats.StringFormat)
    End If

End Sub

Private Sub Listbox1_Mousedown(ByVal Sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
    ListBox1.DoDragDrop(ListBox1.SelectedItems.Item(0), DragDropEffects.Copy Or DragDropEffects.Move)
End Sub

Private Sub ListView1_ItemDrag(sender As System.Object, e As System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag
    ListView1.DoDragDrop(ListView1.SelectedItems(0).Text, DragDropEffects.Copy Or DragDropEffects.Move)

End Sub

Edited 2 Years Ago by Reverend Jim

Thanks to your code, I was finally able to determine what was wrong with mine; it had to do with line 34 of your code

ListView1.DoDragDrop(ListView1.SelectedItems(0).Text, DragDropEffects.Copy Or DragDropEffects.Move)

I was looking for effect 'Copy' but was creating the DoDragDrop with the effect 'Move', VB did not like this. Thanks for all of your help and patience.

This question has already been answered. Start a new discussion instead.