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!!!

Dani AI

Generated

Short summary: the cleanest way to get the behavior you want (show the picture while dragging, and only make the source disappear when a real drop into the trash occurs) is to (1) keep the source intact while the drag runs, (2) start the drag only after a small mouse-move threshold, (3) inspect DoDragDrop’s return value to decide whether to hide/remove the source, and (4) use GiveFeedback to show a custom cursor made from the image while dragging. , the problem you reported (source becoming empty even when the drop is cancelled) is caused by clearing the image before the drag. ’s tip got you halfway there; the pattern below completes it and keeps behavior predictable for students.

Example pattern (VB.NET, WinForms):

Private dragSource As PictureBox
Private startPt As Point
Private originalImage As Image

Private Sub src_MouseDown(sender As Object, e As MouseEventArgs) Handles src.MouseDown
    dragSource = DirectCast(sender, PictureBox)
    originalImage = dragSource.Image
    startPt = e.Location
End Sub

Private Sub src_MouseMove(sender As Object, e As MouseEventArgs) Handles src.MouseMove
    If dragSource Is Nothing OrElse e.Button <> MouseButtons.Left Then Return
    If Math.Abs(e.X - startPt.X) < SystemInformation.DragSize.Width AndAlso Math.Abs(e.Y - startPt.Y) < SystemInformation.DragSize.Height Then Return

    Dim bmp As New Bitmap(originalImage) 'ensure a Bitmap
    Dim data As New DataObject()
    data.SetData(DataFormats.Bitmap, bmp)

    Dim result As DragDropEffects = dragSource.DoDragDrop(data, DragDropEffects.Move)
    If result = DragDropEffects.Move Then
        dragSource.Visible = False   'only hide after a confirmed Move
    Else
        dragSource.Image = originalImage
    End If

    dragSource = Nothing
End Sub

To show the image under the cursor while dragging, create one cursor from the bitmap, hook GiveFeedback to set e.UseDefaultCursors = False and assign Cursor.Current, then remove the handler and dispose the cursor after DoDragDrop returns. Remember to free the icon handle (DestroyIcon via DllImport) after disposing the Cursor to avoid leaks.

Extra tips: start drags only after the SystemInformation.DragSize threshold to avoid false drags; ensure the target returns a Move effect when it accepts the image (that’s how DoDragDrop reports success); and dispose any temporary bitmaps/cursors you create. This keeps the flow simple for students while matching VB6 behavior (visual feedback + only remove source after real drop).

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.