VB Tutor VB.NET 2022 Tutorial VB2019 Tutorial VB6 Tutorial VB Sample Code About Us
Visual Basic Sample Code

Drag and Drop Recycle Bin

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.

Interactive Demo

How to use: Drag any item below to the recycle bin. If you drag the fire item, the bin will start burning!

📄
📁
🖼️
🔥
🎵
🎬
Recycle Bin
Items recycled: 0

How It Works

VB6 Implementation

In Visual Basic 6, drag and drop functionality is implemented by:

  • Setting the DragMode property of controls to 1 (Automatic)
  • Handling the DragDrop event of the target control
  • Using the Source parameter to identify the dragged control
  • Changing the Picture property to show the burning bin when fire is dropped

VB.NET Implementation

In VB.NET, drag and drop uses a different approach:

  • Setting AllowDrop property to True for the target control
  • Handling MouseDown event to start drag operation
  • Using DragEnter to set the effect
  • Handling DragDrop to process the dropped item
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

VB6 vs VB.NET Drag and Drop

VB6 Approach

Uses automatic drag mode with DragMode property set to 1. Simpler but less flexible.

VB.NET Approach

Uses DoDragDrop method with event handlers for more control over drag operations.

Pros of VB6 Approach

  • Simple to implement with minimal code
  • Built-in drag icon functionality
  • Good for basic drag and drop needs

Pros of VB.NET Approach

  • More control over drag operations
  • Better event handling for drag enter/leave
  • Supports custom drag images and effects