none
How do I Drag Drop the address from Windows Explorer into a Text Box in a form? RRS feed

  • Question

  • I am developing a windows form application in Visual Basic and I need to Drag and Drop the address from a Windows Explorer address bar into a TextBox in my form. I have Drag and Drop working but only for Text. I can't find out how to get the text box to accept the address drop.  I also tried this on a Rich TextBox and that does not work either.

    Does anyone have insight into what I need to do?  I have searching the forum and MSDN etc. but can't seem to find anything that addresses this.

    Friday, August 3, 2018 5:30 PM

Answers

  • This should work:

    Private Sub TextBox1_DragEnter(sender As Object, e As DragEventArgs) Handles TextBox1.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub
    
    Private Sub TextBox1_DragDrop(sender As Object, e As DragEventArgs) Handles TextBox1.DragDrop
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            TextBox1.Text = CType(e.Data.GetData(DataFormats.FileDrop), String())(0)
        End If
    End Sub
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by tommytwotrain Friday, August 3, 2018 10:01 PM
    • Marked as answer by TJBlues Saturday, August 4, 2018 2:26 AM
    Friday, August 3, 2018 8:24 PM
    Moderator

All replies

  • Which Windows Explorer do you mean? Internet or file?

    Either way it appears you need to use the clipboard to copy that text. ie cut and paste.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.gettext(v=vs.110).aspx


    Friday, August 3, 2018 6:40 PM
  • I need to get the address from the address bar of Windows File Explorer.  I looked at your reference and that is not good.  I can cut and paste and that is what I have been using but it is time consuming and really want to use drag and drop.
    Friday, August 3, 2018 7:03 PM
  • This should work:

    Private Sub TextBox1_DragEnter(sender As Object, e As DragEventArgs) Handles TextBox1.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub
    
    Private Sub TextBox1_DragDrop(sender As Object, e As DragEventArgs) Handles TextBox1.DragDrop
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            TextBox1.Text = CType(e.Data.GetData(DataFormats.FileDrop), String())(0)
        End If
    End Sub
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by tommytwotrain Friday, August 3, 2018 10:01 PM
    • Marked as answer by TJBlues Saturday, August 4, 2018 2:26 AM
    Friday, August 3, 2018 8:24 PM
    Moderator
  • I need to get the address from the address bar of Windows File Explorer.  I looked at your reference and that is not good.  I can cut and paste and that is what I have been using but it is time consuming and really want to use drag and drop.

    If you mean that you cannot drag a file system path out of the Windows Explorer address bar then that is a design limitation of Windows Explorer.  If it doesn't support dragging I don't think there is anything that your VB.Net application can do to change that.
    Friday, August 3, 2018 9:20 PM

  • If you mean that you cannot drag a file system path out of the Windows Explorer address bar then that is a design limitation of Windows Explorer.  If it doesn't support dragging I don't think there is anything that your VB.Net application can do to change that.
    Yes it does, you just have to click to the left of the highlighted text near the icon... you can't click the highlight itself.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, August 3, 2018 9:40 PM
    Moderator

  • If you mean that you cannot drag a file system path out of the Windows Explorer address bar then that is a design limitation of Windows Explorer.  If it doesn't support dragging I don't think there is anything that your VB.Net application can do to change that.

    Yes it does, you just have to click to the left of the highlighted text near the icon... you can't click the highlight itself.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    LOL yes, after Reed posted I saw that you drag the icon off the tool bar not the text. I have not tried it. Not sure what it returns exactly.

    However one can also drag the file name from the other areas of explorer so not sure what is so special.

    Friday, August 3, 2018 10:04 PM

  • If you mean that you cannot drag a file system path out of the Windows Explorer address bar then that is a design limitation of Windows Explorer.  If it doesn't support dragging I don't think there is anything that your VB.Net application can do to change that.

    Yes it does, you just have to click to the left of the highlighted text near the icon... you can't click the highlight itself.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    LOL yes, after Reed posted I saw that you drag the icon off the tool bar not the text. I have not tried it. Not sure what it returns exactly.

    However one can also drag the file name from the other areas of explorer so not sure what is so special.

    A quick test showed that the data object enumerated the following formats :

    [3748] Dialog HWND is 202CC
    [3748] Clipboard format is 0xc0b7, Name is Shell IDList Array
    [3748] Clipboard format is 0xc0c1, Name is Preferred DropEffect
    [3748] Clipboard format is 0xc27c, Name is UsingDefaultDragImage
    [3748] Clipboard format is 0xc27d, Name is DragImageBits
    [3748] Clipboard format is 0xc0c7, Name is DragContext
    [3748] Clipboard format is 0xc0c6, Name is InShellDragLoop
    [3748] Clipboard format is 0x0f, Name is Predefined Clipboard format
    [3748] Clipboard format is 0xc006, Name is FileName
    [3748] Clipboard format is 0xc007, Name is FileNameW
    [3748] Clipboard format is 0xc134, Name is IsShowingLayered
    [3748] Clipboard format is 0xc133, Name is DragWindow
    [3748] Clipboard format is 0xc281, Name is IsComputingImage
    [3748] Clipboard format is 0xc0d0, Name is DropDescription
    [3748] Clipboard format is 0xc0d1, Name is DisableDragText
    [3748] Clipboard format is 0xc280, Name is IsShowingText
    [3748] Clipboard format is 0xc284, Name is ComputedDragImage
    

    Friday, August 3, 2018 11:04 PM
  • Thanks Reed that works perfectly.  Now to do some research to figure out why I couldn't figure that out myself.
    • Edited by TJBlues Saturday, August 4, 2018 2:28 AM Creditied wrong person for answer
    Saturday, August 4, 2018 2:27 AM
  • Because every other use of drag and drop is implemented by clicking and dragging the item, not some point that is adjacent to the item we want to drag.  As they say, its counter-intuitive.
    Saturday, August 4, 2018 2:33 AM