Interactive demo with VB6 and VB.NET code examples
This interactive demonstration showcases a drag and drop functionality that simulates a recycle bin, similar to Windows file operations. The implementation includes both VB6 and VB.NET code examples.
In this demo, you can drag various items into the recycle bin. When you drag the fire item into the bin, it will start burning. Try dragging the other items to see them disappear into the bin.
How to use: Drag any item below to the recycle bin. If you drag the fire item, the bin will start burning!
In Visual Basic 6, drag and drop functionality is implemented by:
In VB.NET, drag and drop uses a different approach:
Private Sub Form_Load() ' Set drag mode for all items imgDocument.DragMode = vbAutomatic imgFolder.DragMode = vbAutomatic imgImage.DragMode = vbAutomatic imgFire.DragMode = vbAutomatic imgFire.Tag = "Fire" ' Tag fire for identification ' Hide burning bin at startup imgBurningBin.Visible = False End Sub Private Sub imgRecycleBin_DragDrop(Source As Control, X As Single, Y As Single) ' Hide the dragged item Source.Visible = False ' If fire is dragged, show burning bin If Source.Tag = "Fire" Then imgRecycleBin.Picture = imgBurningBin.Picture End If End Sub Private Sub cmdReset_Click() ' Reset all items and bin imgDocument.Visible = True imgFolder.Visible = True imgImage.Visible = True imgFire.Visible = True imgRecycleBin.Picture = LoadPicture("recycle_bin.png") End Sub
Public Class RecycleBinForm Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Allow the recycle bin to accept drops picRecycleBin.AllowDrop = True End Sub Private Sub DraggableItem_MouseDown(sender As Object, e As MouseEventArgs) Handles _ picDocument.MouseDown, picFolder.MouseDown, picImage.MouseDown, picFire.MouseDown ' Start drag operation Dim pb As PictureBox = CType(sender, PictureBox) pb.DoDragDrop(pb, DragDropEffects.Move) End Sub Private Sub picRecycleBin_DragEnter(sender As Object, e As DragEventArgs) Handles picRecycleBin.DragEnter ' Check if the data is a PictureBox If e.Data.GetDataPresent(GetType(PictureBox)) Then e.Effect = DragDropEffects.Move End If End Sub Private Sub picRecycleBin_DragDrop(sender As Object, e As DragEventArgs) Handles picRecycleBin.DragDrop ' Get the dropped PictureBox Dim pb As PictureBox = CType(e.Data.GetData(GetType(PictureBox)), PictureBox) ' Hide the dragged item pb.Visible = False ' If it's the fire, change the bin image If pb.Name = "picFire" Then picRecycleBin.Image = My.Resources.burning_recycle_bin End If End Sub Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click ' Reset all items and bin picDocument.Visible = True picFolder.Visible = True picImage.Visible = True picFire.Visible = True picRecycleBin.Image = My.Resources.recycle_bin End Sub End Class
Uses automatic drag mode with DragMode property set to 1. Simpler but less flexible.
Uses DoDragDrop method with event handlers for more control over drag operations.