I'm trying to convert a simple program fron vb6. IT's only draggin several pictures with the mouse and drop them into anothet picture (trashbin). If the object picture dropped is a match then the trashbin.image changes to another image

But: First, I don't know how to allow drag controls!!!

Recommended Answers

All 5 Replies

First.
In the form load event, add this line: targetPictureBox.AllowDrop = True

For the source picturebox, add this method:

Private Sub sourcePictureBox_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles sourcePictureBox.MouseDown
   Dim pic As PictureBox = DirectCast(sender, PictureBox)
   pic.DoDragDrop(pic.Image, DragDropEffects.Move)
   ' Or if you prefer to just copy the picture
   'pic.DoDragDrop(pic.Image, DragDropEffects.Copy)
End Sub

For the target picturebox, add these two methods:

Private Sub targetPictureBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles targetPictureBox.DragEnter
   If e.Data.GetDataPresent(DataFormats.Bitmap)
      e.Effect = DragDropEffects.All
   End If
End Sub

Private Sub targetPictureBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles targetPictureBox.DragDrop
   If e.Data.GetDataPresent(DataFormat.BitMap) Then
      Dim img As BitMap = e.Data.GetData(DataFormats.BitMap)
      'Insert comparison code here
      targetPictureBox.Image = img
   End If
End Sub

Thank you. It goes but....

I'd need the source gets invisible when dropped exactly into de target (it's suposed to have been droped into a trashbin)

plus, I can't see the sources moving while being draged (dragIcon in VB6)

Ok. Then we need to change some things for the source picturebox.

Private bMouseIsDown As Boolean = False
Private Sub sourcePictureBox_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles sourcePictureBox.MouseDown
   If sourcePictureBox.Image IsNot Nothing Then
      bMouseIsDown = True
   End If
End Sub

Private Sub sourcePictureBox_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles sourcePictureBox.MouseMove
   If bMouseIsDown Then
      Dim pic As PictureBox = DirectCast(sender, PictureBox)
      Dim img As BitMap = pic.Image
      pic.Image = Nothing
      pic.DoDragDrop(img, DragDropEffects.Move)
      ' Or if you prefer to just copy the picture
      'pic.DoDragDrop(img, DragDropEffects.Copy)
   End If
   bMouseIsDown = False
End Sub

And for the target.

Private Sub targetPictureBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles targetPictureBox.DragDrop
   If e.Data.GetDataPresent(DataFormat.BitMap) Then
      Dim img As BitMap = e.Data.GetData(DataFormats.BitMap)
      If img <> sourcePictureBox.Image Then 'I don't know if this one will work
         sourcePictureBox.Image = img 'Restore the picture from the source
      End If
   End If
End Sub

And for the visibility while dragging. I found a discussion on another site that will give you the answer to that one. It will give a complete method for drag and drop of image.
http://www.tek-tips.com/viewthread.cfm?qid=1241165&page=1

hi there...

If img <> sourcePictureBox.Image Then 'I don't know if this one will work

doesnt work but I've tried If not img equals... and it works

But....Source gets nothing either dropped into target or outside.

Anyhow, this comes too complicated in NET for my students. I'm simply translating my old programs from VB6 but i'll discard this. It was much easier and instructive in the earlier version.

Thank you very much anyway.

Regards from Spain

Well. I figured that since you mentioned that the target would be a trashcan, then there would be no point in assigning an image to the target picturebox. That's why changed that line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.