Drag/Drop and Copy Control in Visual Basic 2008

In this tutorial I will teach you how to create Drag/Drop and Copy Control using Visual Basic 2008. With this, you can drag and drop the picture that’s inside the Picture box and you can also copy it to another Picture Box by pressing the Ctrl key control. Let's begin: Open Visual Basic 2008, create a new Windows Application and drag the two PictureBoxes in the Form. Then, name the first PictureBox “leftpic” and the other one is “rightpic”. firstform Double click the Form, and do the following code for declaring a constant variable above the Form_Load.
  1. ' DECLARE A CONSTANT VARIABLE
  2. ' IT IS USED FOR DETECTING THE CTRL KEY WHETHER IT
  3. ' WAS PRESSED OR NOT DURING THE DRAG OPERATION
  4. Const maskCtrl As Byte = 8
In the Form_Load do this following code for activating the AllowDrop property.
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. 'Set the AllowDrop property to the PictureBox
  3. leftpic.AllowDrop = True
  4. rightpic.AllowDrop = True
  5. End Sub
Go back to the Design Views, click the two PictureBoxes and go to the properties. After that, click the events on the top that looks like a lightning. Choose the MouseDown event, then double click it and do this following code.
  1. Private Sub PictureBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles leftpic.MouseDown, rightpic.MouseDown
  2. If e.Button = Windows.Forms.MouseButtons.Left Then
  3. Dim pictures As PictureBox = CType(sender, PictureBox)
  4.  
  5. 'ACTIVATES THE DRAG AND DROP OPERATION
  6. If Not pictures.Image Is Nothing Then
  7. pictures.DoDragDrop(pictures.Image, DragDropEffects.Move Or DragDropEffects.Copy)
  8. End If
  9. End If
  10. End Sub
In the Method Name, choose the DragEnter for the events and do this following code.
  1. Private Sub PictureBox_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles leftpic.DragEnter, rightpic.DragEnter
  2.  
  3. 'CONDITIONING THE DRAG CONTENT TO MAKE SURE THAT THE TYPE IS ACCURATE FOR THIS CONTROL
  4. If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
  5. If (e.KeyState And maskCtrl) = maskCtrl Then 'CHECKING THE CTRL KEY WAS PRESSED.
  6. 'RESULT TRUE
  7. e.Effect = DragDropEffects.Copy 'PERFORM A COPY.
  8. Else
  9. 'RESULT FALSE
  10. e.Effect = DragDropEffects.Move 'PERFORM A MOVE
  11. End If
  12. Else
  13. 'REJECT THE DROP
  14. e.Effect = DragDropEffects.None
  15. End If
  16. End Sub
Go back to the Method Name again,choose DragDrop for the events and do this following code.
  1. Private Sub PictureBox_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles leftpic.DragDrop, rightpic.DragDrop
  2.  
  3. 'IMAGE WILL DISPLAY TO THE SELECTED PICTUREBOX CONTROL.
  4. Dim pictures As PictureBox = CType(sender, PictureBox)
  5. pictures.Image = CType(e.Data.GetData(DataFormats.Bitmap), Bitmap)
  6.  
  7. 'IF THE PICTURE WAS MOVED IN THE PICTUREBOX THE OTHER BOX BECOMES EMPTY.
  8. If (e.KeyState And maskCtrl) <> maskCtrl Then 'CHECKING THE CTRL KEY WAS NOT PRESSED.
  9. If sender Is leftpic Then
  10. rightpic.Image = Nothing
  11. Else
  12. leftpic.Image = Nothing
  13. End If
  14. End If
  15. End Sub
Note: Put an image in one of the two PictureBoxes.

Add new comment