Drag and Drop an Image Between Two Picture Boxes with vb.net

Good Morning! I am battling a case of the flu the entire weekend so I didn’t do too much. Mostly stayed in bed watching episodes of “House” on my DVR. I better get better soon cause  my weekends are about to get a lot busier. So me and DayQuil are friends today!

Today’s topic is how to drag an image between two picture boxes. Pretty straightforward but I got a question about it yesterday so I made it my topic. Make it a great day!


Join me on Facebook

 

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is PictureBox Then
                If Not (ctl Is PictureBox1) Then
                    ctl.AllowDrop = True
                    AddHandler ctl.DragEnter, AddressOf pb_DragEnter
                    AddHandler ctl.DragDrop, AddressOf pb_DragDrop
                End If
            End If
        Next
    End Sub
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim MyData As New DataObject("MyData", PictureBox1.Image)
            PictureBox1.DoDragDrop(MyData, DragDropEffects.Copy)
        End If
    End Sub
    Private Sub pb_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        If e.Data.GetDataPresent("MyData") Then
            e.Effect = DragDropEffects.All
        End If
    End Sub
    Private Sub pb_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        DirectCast(sender, PictureBox).Image = e.Data.GetData("MyData")
    End Sub
End Class

Technorati Tags: ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: