Windows Develop Bookmark and Share   
 index > Windows Forms Designer > DragAndDrop from Computer to PictureBox doesn't work.
 

DragAndDrop from Computer to PictureBox doesn't work.

My example program is very short. It's comprised only of a Form, a Picturebox, and five subs, most are empty.

Here's the code:
Public Class DragAndDropDontWork



    Private Sub PictureBox1_DragDrop(ByVal sender As Object, _

ByVal e As System.Windows.Forms.DragEventArgs) _

Handles PictureBox1.DragDrop



    End Sub



    Private Sub PictureBox1_DragEnter(ByVal sender As Object, _

ByVal e As System.Windows.Forms.DragEventArgs) _

Handles PictureBox1.DragEnter



    End Sub



    Private Sub PictureBox1_DragLeave(ByVal sender As Object, _

ByVal e As System.EventArgs) _

Handles PictureBox1.DragLeave



    End Sub



    Private Sub PictureBox1_DragOver(ByVal sender As Object, _

ByVal e As System.Windows.Forms.DragEventArgs) _

Handles PictureBox1.DragOver

        If Not (e.Data.GetDataPresent(_

GetType (System.Drawing.Image))) Then

            e.Effect = DragDropEffects.None

            Me.PictureBox1.Image = Me.PictureBox1.InitialImage

            Return

        End If

        ' Set the effect based upon the KeyState.

        If ((e.KeyState And (8 + 32)) = (8 + 32) And _

            (e.AllowedEffect And DragDropEffects.Link) _

= DragDropEffects.Link) Then

            ' KeyState 8 + 32 = CTL + ALT



            ' Link drag-and-drop effect.

            e.Effect = DragDropEffects.Link



        ElseIf ((e.KeyState And 32) = 32 And _

            (e.AllowedEffect And DragDropEffects.Link) _

= DragDropEffects.Link) Then



            ' ALT KeyState for link.

            e.Effect = DragDropEffects.Link



        ElseIf ((e.KeyState And 4) = 4 And _

            (e.AllowedEffect And DragDropEffects.Move) _

= DragDropEffects.Move) Then



            ' SHIFT KeyState for move.

            e.Effect = DragDropEffects.Move



        ElseIf ((e.KeyState And 8) = 8 And _

            (e.AllowedEffect And DragDropEffects.Copy) _

= DragDropEffects.Copy) Then



            ' CTL KeyState for copy.

            e.Effect = DragDropEffects.Copy



        ElseIf ((e.AllowedEffect And DragDropEffects.Move) _

= DragDropEffects.Move) Then



            ' By default, the drop action should be move, if allowed.

            e.Effect = DragDropEffects.Move



        Else

            e.Effect = DragDropEffects.None

        End If

    End Sub



    Private Sub DragAndDropDontWork_Load( _

ByVal sender As Object, ByVal e As System.EventArgs) _

Handles Me.Load

        Me.AllowDrop = True

    End Sub

End Class




If you'll set a break point on each sub and run the app under the debugger, you'll notice that dropping a bitmap into the picturebox doesn't fire any of the events.

I hope someone can help me with this, as I have a bad headache and I need to get to work on building a new brick wall.

Thanks,
BLeRg.
iamBLeRg  Saturday, May 23, 2009 6:57 PM
Wow, that took a while. My day's getting a little long in the tooth, but times not up yet. The solution to my problem is:
    Private Sub PictureBox1_DragDrop( _

ByVal sender As Object,  _

ByVal e As System.Windows.Forms.DragEventArgs) _

 Handles PictureBox1.DragDrop

        ' Handle FileDrop data.

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

 Then

            ' Assign the file names to a string array, _

            ' in case the user has selected multiple  _

            'files.

            Dim files As String() = CType( _

e.Data.GetData(DataFormats.FileDrop), String())

            Me.fileName = files(0)

            Try

                ' Assign the first image to the  _

                'picture' variable.

                Me.PictureBox1.Image = Image.FromFile( _

 files(0))

              Catch ex As Exception

                MessageBox.Show(ex.Message)

                Return

            End Try

        End If



        ' Handle Bitmap data.

        If e.Data.GetDataPresent(DataFormats.Bitmap)  _

Then

            Try

                ' Create an Image and assign it to the  _

picture variable.

                Me.PictureBox1.Image = CType( _

e.Data.GetData(DataFormats.Bitmap), Image)

            Catch ex As Exception

                MessageBox.Show(ex.Message)

                Return

            End Try

        End If



        ' Force the form to be redrawn with the image.

        Me.PictureBox1.Invalidate()

    End Sub



    Private Sub PictureBox1_DragEnter(ByVal sender  _

As Object,  _

ByVal e As System.Windows.Forms.DragEventArgs)  _

Handles PictureBox1.DragEnter

        ' Check to be sure that the drag content is  _

         'the correct type for this 

        ' control. If not, reject the drop.

        ' If the data is a file or a bitmap, display  _

         the copy cursor.

        If e.Data.GetDataPresent(DataFormats.Bitmap) _

           Or e.Data.GetDataPresent( _

DataFormats.FileDrop) Then

            e.Effect = DragDropEffects.Copy

        Else

            e.Effect = DragDropEffects.None

        End If

    End Sub


Although the keyboard and mouse quit responding a couple of times, forcing me to hold the power button down, I can now consider this matter closed.

Thanks,
BLeRg.
  • Marked As Answer byiamBLeRg Sunday, May 24, 2009 6:02 PM
  • Edited byiamBLeRg Sunday, May 24, 2009 6:08 PMAdd info to last sentence.
  •  
iamBLeRg  Sunday, May 24, 2009 5:59 PM
Well, I found a sample (DragDrop). Sample works why won't my app? I discovered that the picturebox control must have its' allowdrop property set explicetly in code. WaitOnLoad = False. Here's the new code:
    Private Sub PictureBox1_DragDrop( _
ByVal sender As Object,  _
ByVal e As System.Windows.Forms.DragEventArgs)  _
Handles PictureBox1.DragDrop
        ' Display the image in the selected PictureBox 
        'control.
        Dim pic As PictureBox =  _
CType(sender, PictureBox)
        pic.Image =  _
CType(e.Data.GetData(DataFormats.Bitmap), Bitmap)
    End Sub

    Private Sub PictureBox1_DragEnter( _
ByVal sender As Object,  _
ByVal e As System.Windows.Forms.DragEventArgs)  _
Handles PictureBox1.DragEnter
        ' Check to be sure that the drag content is  _
         'the correct type for this control. If not,  _
         'reject the drop.


        'If (e.Data.GetDataPresent( _
        'DataFormats.Bitmap)) Then

        ' If the Ctrl key was pressed during the drag  _
        'operation then perform a Move. If not,  _
        'perform a Copy.
        If (e.KeyState And CtrlMask) = CtrlMask Then
            e.Effect = DragDropEffects.Move
        Else
            e.Effect = DragDropEffects.Copy
        End If
        'Else
        'e.Effect = DragDropEffects.None
        'End If
    End Sub

    Private Sub PictureBox1_DragOver( _
ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)  _
Handles PictureBox1.DragOver
        ' Display the image in the selected  _
        'PictureBox control.
        Dim pic As PictureBox = CType( _
sender, PictureBox)
        pic.Image = CType(e.Data.GetData( _
DataFormats.Bitmap), Bitmap)
    End Sub

I really need some help with this because I've been at for a couple maybe three hours with not much success. The DragDrop event doesn't fire. The DragEnter event fires, but 'If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then' doesn't recognize when a bitmap is dropped, so I commented it out in order that the inner if block could execute.

I'm going to see if I can drag a bitmap out of MyComputer onto the DragDrop sample that I downloaded. I'll also debug it to see what the 'Data' look like.

Thanks,
BLeRg.

iamBLeRg  Sunday, May 24, 2009 11:31 AM
Nope. Can't drop an image dragged from MyComputer onto the 'Drag and Drop Sample' Pictureboxs. The Pictureboxs in the sample aquire the bitmap image when 'If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then' is executed. So any ideas?

Thanks,
BLeRg.
iamBLeRg  Sunday, May 24, 2009 11:45 AM
Ah. Okay. 'If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then' allows the code in the if block to execute. Not exactly what I want. So now I need to find an app (such as MSPAINT.EXE) which can accept a drag and drop operation from Computer/MyComputer. I'll let you know what I find, but I'm thinking that mc works with files and most other apps (such as mspaint work with images) work with other types, so I'm probably going to have to modify the app so it includes a kind of browser pane to sub for mc.

Thanks,
BLeRg.
iamBLeRg  Sunday, May 24, 2009 1:35 PM
I was (happily) wrong. MSPAINT is a perfect example of what I want to do. So now, how?

Thanks,
BLeRg.
iamBLeRg  Sunday, May 24, 2009 1:41 PM
Wow, that took a while. My day's getting a little long in the tooth, but times not up yet. The solution to my problem is:
    Private Sub PictureBox1_DragDrop( _

ByVal sender As Object,  _

ByVal e As System.Windows.Forms.DragEventArgs) _

 Handles PictureBox1.DragDrop

        ' Handle FileDrop data.

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

 Then

            ' Assign the file names to a string array, _

            ' in case the user has selected multiple  _

            'files.

            Dim files As String() = CType( _

e.Data.GetData(DataFormats.FileDrop), String())

            Me.fileName = files(0)

            Try

                ' Assign the first image to the  _

                'picture' variable.

                Me.PictureBox1.Image = Image.FromFile( _

 files(0))

              Catch ex As Exception

                MessageBox.Show(ex.Message)

                Return

            End Try

        End If



        ' Handle Bitmap data.

        If e.Data.GetDataPresent(DataFormats.Bitmap)  _

Then

            Try

                ' Create an Image and assign it to the  _

picture variable.

                Me.PictureBox1.Image = CType( _

e.Data.GetData(DataFormats.Bitmap), Image)

            Catch ex As Exception

                MessageBox.Show(ex.Message)

                Return

            End Try

        End If



        ' Force the form to be redrawn with the image.

        Me.PictureBox1.Invalidate()

    End Sub



    Private Sub PictureBox1_DragEnter(ByVal sender  _

As Object,  _

ByVal e As System.Windows.Forms.DragEventArgs)  _

Handles PictureBox1.DragEnter

        ' Check to be sure that the drag content is  _

         'the correct type for this 

        ' control. If not, reject the drop.

        ' If the data is a file or a bitmap, display  _

         the copy cursor.

        If e.Data.GetDataPresent(DataFormats.Bitmap) _

           Or e.Data.GetDataPresent( _

DataFormats.FileDrop) Then

            e.Effect = DragDropEffects.Copy

        Else

            e.Effect = DragDropEffects.None

        End If

    End Sub


Although the keyboard and mouse quit responding a couple of times, forcing me to hold the power button down, I can now consider this matter closed.

Thanks,
BLeRg.
  • Marked As Answer byiamBLeRg Sunday, May 24, 2009 6:02 PM
  • Edited byiamBLeRg Sunday, May 24, 2009 6:08 PMAdd info to last sentence.
  •  
iamBLeRg  Sunday, May 24, 2009 5:59 PM

You can use google to search for other answers

Custom Search

More Threads

• Removing (Disabling) the veritical scrollbar in a listbox
• works on VS2005 Machines and does not work on NON VS2005 machines
• ComboBox list - display first item
• how to add one more control in open dialog box
• Calls to the set and get methods of a property
• How to change the width of the tool tip.
• Relating Combos on Windows Forms
• Property isn't displayed
• changing default selection color on mouse hover in dropdown menu item
• Google Like deskbar application