Drag and drop a file to a picturebox

mooman_fl

Centurion
Joined
Nov 3, 2002
Messages
193
Location
Florida, USA
Greetings all,

Needing to be able to drag and drop files or even weblinks from Explorer or a browser into a picturebox and get the string for the path or text of the link. I think I could figure out how to get the text if I could just figure out how to accomplish this drag and drop feat. Have searched the web for examples figuring there would be one out there but they all deal with dragging and dropping from one picturebox to another, or even to the form itself. I just want it limited to the picturebox control on the form, and the dragging will definitely be coming from outside the program.

Any pointers or examples (if it is simple and won't take up too much of your time) would be helpful.

Thanks in advance.

EDIT:

Decided to edit and give an example of what I tried that didn't work based on the little I could find on the web...

Code:
Public Class Form1

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        Me.pbxRoboInstall.AllowDrop = True

    End Sub

    Private Sub pbxRoboInstall_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles pbxRoboInstall.DragDrop
        Dim s() As String = e.Data.GetData("FileDrop", False)
        Dim i As Integer
        For i = 0 To s.Length - 1
            MessageBox.Show(s(i).ToString)
        Next i
    End Sub

End Class
 
Last edited:
You have to love it when you post to ask for help after tearing your hair out all night and figure it out for yourself less than 15 minutes later.

The code I was missing was the following:

Code:
    'Lets the program know a possible DragDrop is being initiated.

    Private Sub pbxRoboInstall_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles pbxRoboInstall.DragEnter

        ' Changes the cursor to drag cursor and checks data being dragged for right types.

        If e.Data.GetDataPresent(DataFormats.FileDrop) Or e.Data.GetDataPresent(DataFormats.Text) Then

            ' If the right types are found allow them to be used by DragDrop event.

            e.Effect = DragDropEffects.Copy
        Else

            ' If they aren't the right type don't let them be used.

            e.Effect = DragDropEffects.None
        End If


    'Handles the actual drop of the data.

    Private Sub pbxRoboInstall_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles pbxRoboInstall.DragDrop

        'If it is a file....

        If e.Data.GetDataPresent(DataFormats.FileDrop) Then

            'Transfer data to an array for processing possible multiple files

            Dim s() As String = e.Data.GetData(DataFormats.FileDrop, False)
            Dim i As Integer

            'iterate through the files and send path to messagebox.

            For i = 0 To s.Length - 1
                MessageBox.Show(s(i).ToString)
            Next i

            'Or, if it is a link...

        ElseIf e.Data.GetDataPresent(DataFormats.Text) Then

            'Get address from the link and put it in a messagebox.

            Dim s As String = e.Data.GetData(DataFormats.Text, False)
            MessageBox.Show(s)
        End If
    End Sub
 
Back
Top