by ICE (35 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: VB 6.0
Difficulty: Beginner
Date Added: Fri 12th February 2021
Rating: (0 Votes)
In this I am going to walk you through making your own VB6 Drag-&-Drop Program.
I will do my best to make it easy to understand & to follow along.
First lets just touch on some things you should know, For example,
The diffrence between the Source & the Target in a Drag-&-Drop Program
There's a difference between the DragOver event & the DragDrop event.
In a Drag & Drop program a object is moved from 1 place to another
The object being moved (dragged) is called the "source". The place it is
being dragged to is called the "target"
Here is a small short quick example of that.
Open a new form,
Add 2 icons to the form, 1 say is some kind of paper,
The other could be a waste can or a paper shredder.
The paper would be the "Source", To make the source moveable,
Set its "DragMode property" to 1 - Automatic,
And set its DragIcon property yo display an icon, you will only see a outline
of it when you drag it.
As you drag the icon across the form, Events are jappening. The events are
for the target not the scorce. As you Drag the icon across the form, the form's
"DragOver event" occurs. If you drag the icon across a control like Command1
button, the command button's DragOver event occurs. As long as keep moving
while you kssp the mousebutton down, DragOver events happen for the form
and every control you move over. When you release the mousebutton with the
icon over a control, the control's "DragDrop" event happens: But if you re-lease
the mouse button over the form, the form's DragDrop event happens.
That means any drag & drop actions. multiple DragOver events may happen.
Only 1 DragDrop event happens when the action is complete. All the events
happen to the target objects, Mot to the source object.
NOTE: You can also code for events that happen for the source object, like
the Click event & the MouseDown, MouseUp & MouseMove events.
That brief introduction focused on the events for the target objects.
Now this example you'll denelope a small applicarion that lets the user drag
a plane icon intia hangar (use a picture box to keep it simple)
You should have 2 plane icons and a picturebox on your form & 1 plane should
be in the picturebox (the picture box is the target),,,
I make a lot of my own icons, Hell 10 or so years ago I made a Tetris game for
a woman an asked her for a pic of herself so I could make it into a icon & put
it on the Tetris game for her, She loved it when she saw it.
OK back to the Drag & Drop application now.
The Form should have 1 PictureBox on it (Name it "Target")
There should be a PlaneParked in the taget, it should be Not Visible
There should also be another plane on the form, it should be Visible.
Now that you should have your form pretty much set up, an almost ready to
test to see if when you drag the plane over the picturebox that it disappears
behind the control (cause of the way graphic controls are layered on rhe form)
Put a label under the PictureBox set its caption to "Hangar"
Lets set some Property's now.....
Fotm Name: frmPlane
Caption: Fly the Plane to the Hangar
The Plane on the form (Not the 1 in the picturebox)
Name: imgPlane
Stretch: TRUE
Picture: Plane.ico
DragIcon: Plane.ico
DragMode: 1 - Automatic
Now lets do the plane that's in the Hangar
Name: imgPlaneParked
Stretch: TRUE
Picture Plane.ico
Visible: False
Set the Name for the PictureBox to,,,, picHangar
The label should say Hangar
There is a reason that you should have all the names an other peoperty's
set as I put them, That is so they will fit the code perfecttly
And we will be writing the code next....
Before writing the code Click the Run & drag the plan to the Hangar
You should see an Icon as you drag & the original image can still
be seen, The next step will make the source image invisible
when you start to drag the image across the form
Code the Drag Action....
In the code window, click on Form for the object & DragOver
for the procedures
Code the following remark & line of code in the Form_DrafOver
event.
' Make the Plane iinvisible
imgPlane.Visible = False
Run the program & see if when youi start dragging the plane that
the original should dissapear & it should look like you're actually dragging
the image, Try dropping it anywhere, it disappears doesn't it...
Now it's time to take care of the drop operation
Click on the stop button....
Lets code the Drop Operation
Code the DragDrop event for the picturebox (picHangar).
Private Sub picHangar_DragDrop(Source As Control, X As Single, Y As Single)
' Good Drop
imgPlaneParked.Visible = True
End Sub
Code the DragDrop event for the form
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
' Bad Drop
imgPlane.Visible = True
End Sub
Code a DragDrop event for the label control. If the user drops the plane
on the label, it's another bad drop.
Private Sub Label1_DragDrop(Source As Control, X As Single, Y As Single)
' Bad Drop
imgPlane.Visible = True
End Sub
' Let me know how it goes