ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

How to DragDrop and Copy Control in VB.Net

In this tutorial, I will teach you how to create Drag/Drop and Copy Control in VB.Net. With this, you can move and drop the picture that’s inside the PictureBox to another PictureBox and you can also copy it by pressing the Ctrl key on the keyboard of the computer.

 

 

Let’s begin:
Open the Visual Basic, Create a new Windows Form Application and drag the two picture boxes in the Form. Then, name the first picture box “lpic” and the other one is “rpic”.
drogdropform1

After setting the Form and naming the two PictureBoxes, go to the solution explorer and click the code view.
drogdropform2
In the code view, you have to declare a constant variable that is needed for detecting the CRLT key on the keyboard.

  1. ' DECLARE A CONSTANT VARIABLE TO USED FOR DETECTING THE CTRL KEY WHEN IT
  2. ' WAS PRESSED DURING THE DRAG OPERATION
  3. Const MASKCTRL As Byte = 8

After that, go back to the Form Design and double click it to fire the Form_Load event handler. Then, you have to activate the AllowDrop property of the PictureBox.

  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. 'SET THE PROPERTIES OF THE PICTUREBOX FOR ALLOWING TO DROP.
  3. lpic.AllowDrop = True
  4. rpic.AllowDrop = True
  5. End Sub

Go back to the Form Design again and click the two picture boxes on the Form. Go to the properties and click the events on the top that looks like a lightning. Choose the MouseDown event and double click it.
drogdropform3
After that, you have to activate the drag and drop operation.

  1. Private Sub PictureBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lpic.MouseDown, rpic.MouseDown
  2. If e.Button = Windows.Forms.MouseButtons.Left Then
  3. Dim pictures As PictureBox = CType(sender, PictureBox)
  4.  
  5. 'IT WILL ACTIVATE 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

After activating the drag and drop operation, go to the method name and choose the DragEnter for the events.
drogdropform4
Now, you have to activate the copy control of the picture.

  1. Private Sub PictureBox_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lpic.DragEnter, rpic.DragEnter
  2.  
  3. 'CHECKING THE DRAG CONTENT TO MAKE SURE THAT, THE TYPE IS ACCURATE FOR THIS CONTROL
  4. If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
  5. 'CHECKING IF THE CTRL KEY WAS PRESSED
  6. 'AND IF THE CTRL KEY IS PRESS PERFORM COPY AND IT IS NOT PERFORM MOVE.
  7. If (e.KeyState And MASKCTRL) = MASKCTRL Then
  8.  
  9. e.Effect = DragDropEffects.Copy
  10. Else
  11.  
  12. e.Effect = DragDropEffects.Move
  13. End If
  14. Else
  15. 'THE DROP EFFECT IS REJECTED.
  16. e.Effect = DragDropEffects.None
  17. End If
  18. End Sub

Go back to the method name again ,choose DragDrop for the events and you have to activate the move control of the picture.

  1. Private Sub PictureBox_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lpic.DragDrop, rpic.DragDrop
  2.  
  3. 'THE IMAGE WILL DISPLAY TO THE SELECTED PICTUREBOX.
  4. Dim PIC As PictureBox = CType(sender, PictureBox)
  5. PIC.Image = CType(e.Data.GetData(DataFormats.Bitmap), Bitmap)
  6.  
  7. 'IF THE PICTURE IN THE PICTUREBOX WAS MOVE, THE OTHER BOX BECOMES EMPTY.
  8. If (e.KeyState And MASKCTRL) <> MASKCTRL Then 'CHECKING THE CTRL KEY WAS NOT PRESSED.
  9. If sender Is lpic Then
  10. rpic.Image = Nothing
  11. Else
  12. lpic.Image = Nothing
  13. End If
  14. End If
  15. End Sub

You can download the complete source code.

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!