Form_Load
.
' DECLARE A CONSTANT VARIABLE ' IT IS USED FOR DETECTING THE CTRL KEY WHETHER IT ' WAS PRESSED OR NOT DURING THE DRAG OPERATION Const maskCtrl As Byte = 8
Form_Load
do this following code for activating the AllowDrop property.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Set the AllowDrop property to the PictureBox leftpic.AllowDrop = True rightpic.AllowDrop = True End Sub
MouseDown
event, then double click it and do this following code.
Private Sub PictureBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles leftpic.MouseDown, rightpic.MouseDown If e.Button = Windows.Forms.MouseButtons.Left Then Dim pictures As PictureBox = CType(sender, PictureBox) 'ACTIVATES THE DRAG AND DROP OPERATION If Not pictures.Image Is Nothing Then pictures.DoDragDrop(pictures.Image, DragDropEffects.Move Or DragDropEffects.Copy) End If End If End Sub
DragEnter
for the events and do this following code.
Private Sub PictureBox_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles leftpic.DragEnter, rightpic.DragEnter 'CONDITIONING THE DRAG CONTENT TO MAKE SURE THAT THE TYPE IS ACCURATE FOR THIS CONTROL If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then If (e.KeyState And maskCtrl) = maskCtrl Then 'CHECKING THE CTRL KEY WAS PRESSED. 'RESULT TRUE e.Effect = DragDropEffects.Copy 'PERFORM A COPY. Else 'RESULT FALSE e.Effect = DragDropEffects.Move 'PERFORM A MOVE End If Else 'REJECT THE DROP e.Effect = DragDropEffects.None End If End Sub
DragDrop
for the events and do this following code.
Private Sub PictureBox_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles leftpic.DragDrop, rightpic.DragDrop 'IMAGE WILL DISPLAY TO THE SELECTED PICTUREBOX CONTROL. Dim pictures As PictureBox = CType(sender, PictureBox) pictures.Image = CType(e.Data.GetData(DataFormats.Bitmap), Bitmap) 'IF THE PICTURE WAS MOVED IN THE PICTUREBOX THE OTHER BOX BECOMES EMPTY. If (e.KeyState And maskCtrl) <> maskCtrl Then 'CHECKING THE CTRL KEY WAS NOT PRESSED. If sender Is leftpic Then rightpic.Image = Nothing Else leftpic.Image = Nothing End If End If End Sub