100 likes | 507 Views
Drag-and-Drop. Differentiate between source and target in a drag-and-drop operationDrag-and-Drop TerminologyDragging and Dropping Multiple ObjectsCreate a program incorporating drag-and-drop. Drag and Drop Terminology. Source" is object being draggedTarget" is location to which source is being
E N D
1. Chapter 13 Drag-and-Drop
2. Drag-and-Drop Differentiate between source and target in a drag-and-drop operation
Drag-and-Drop Terminology
Dragging and Dropping Multiple Objects
Create a program incorporating drag-and-drop
3. Drag and Drop Terminology Source is object being dragged
Target is location to which source is being dragged
Source Object properties of interest:
DragMode: Automatic
DragIcon: Causes source to be displayed as it is dragged
4. More Drag/Drop Terms Form and control events occur as object is dragged over them
Targets include forms, buttons, and others over which source object passes
Events: DragOver and DragDrop
DragOver event occurs as you drag mouse over a form or control
DragDrop event occurs when you release the left mouse button after dragging an object DragOver event occurs continuously as you drag an object over a form.
If an object is dragged over a button on a form, then the DragOver event occurs for the button, not the form.
As long as you keep the mouse button down and keep the mouse moving, DragOver events occur for the form and every control over which you move the mouse (and thus, the source).
If you release the mouse over a form, the forms DragDrop event occurs.
For any drag and drop operation, multiple DragOver events occur, but only one DragDrop event occurs.DragOver event occurs continuously as you drag an object over a form.
If an object is dragged over a button on a form, then the DragOver event occurs for the button, not the form.
As long as you keep the mouse button down and keep the mouse moving, DragOver events occur for the form and every control over which you move the mouse (and thus, the source).
If you release the mouse over a form, the forms DragDrop event occurs.
For any drag and drop operation, multiple DragOver events occur, but only one DragDrop event occurs.
5. Step-by-Step Example (plane) Application allows user to drag airplane from one place to another (the hanger)
After creating two images controls and a frame control, set the DragMode of source to Automatic
Next, set the DragDrop and DragOver properties of objects including the form
Forms DragOver event procedure should make leftmost plane invisible
Forms DragDrop event procedure should make original planes image visible.
Picture boxs DragDrop event procedure should make hanger planes image visible.
Finally, labels (Hanger) DragDrop event procedure should make original planes image visible.
Forms DragOver event procedure should make leftmost plane invisible
Forms DragDrop event procedure should make original planes image visible.
Picture boxs DragDrop event procedure should make hanger planes image visible.
Finally, labels (Hanger) DragDrop event procedure should make original planes image visible.
6. Dragging & Dropping Multiple Objects Identify multiple drag/drop sources with a control array.
Code DragDrop code for bad drops in a separate routine called from individual controls that call the code to avoid code duplication.
Pass the source object as an argument to determine which one becomes invisible.
When a DragOver event occurs, the DragOver event procedure passes the name of the source object causing the event.
Write a single Bad drop routine that handles all the DragDrop events so that the code is not duplicated in the DragDrop event of every object.
When a DragOver event occurs, the DragOver event procedure passes the name of the source object causing the event.
Write a single Bad drop routine that handles all the DragDrop events so that the code is not duplicated in the DragDrop event of every object.
7. Dragging with Multiple Sources Make each object's DragDrop event (bad drop) call common code
Make the target images picture property equal to the source images picture property on a good drop
Set the DragIcon property to the picture property of the source
Bad drop code (place a call to this code in DragDrop event procedure of all controls that should not receive the source object on a drop.):
Private Sub BadDrop(Source As Control)
'Common code that is executed when source
'is dropped on a control that it shouldn't
Source.Visible = True 'keep source visible
End Sub
Code for object(s) that can receive the DragDrop event:
Private Sub imgTarget_DragDrop(Source As Control, X As Single, _
Y As Single)
'Target has received object: good drop
'Make target picture same as source causing event:
imgTarget.Picture = Source.Picture
End Sub
You can load the DragIcon of each of several objects at form load time from the picture property of the source so that the image is not loaded from disk multiple times. Example code is as follows:
Private Sub Form_Load()
'Set DragIcon property of source at load time
Dim intCtr As Integer
For intCtr = 0 To imgToy.Count - 1
imgToy(intCtr).DragIcon = imgToy(intCtr).Picture
Next
End SubBad drop code (place a call to this code in DragDrop event procedure of all controls that should not receive the source object on a drop.):
Private Sub BadDrop(Source As Control)
'Common code that is executed when source
'is dropped on a control that it shouldn't
Source.Visible = True 'keep source visible
End Sub
Code for object(s) that can receive the DragDrop event:
Private Sub imgTarget_DragDrop(Source As Control, X As Single, _
Y As Single)
'Target has received object: good drop
'Make target picture same as source causing event:
imgTarget.Picture = Source.Picture
End Sub
You can load the DragIcon of each of several objects at form load time from the picture property of the source so that the image is not loaded from disk multiple times. Example code is as follows:
Private Sub Form_Load()
'Set DragIcon property of source at load time
Dim intCtr As Integer
For intCtr = 0 To imgToy.Count - 1
imgToy(intCtr).DragIcon = imgToy(intCtr).Picture
Next
End Sub
8. Blanking an Image Control Two ways to blank an image control:
imgTarget.picture = LoadPicture("")
imgTarget.Picture = imgBlank.Picture
If the second approach is used, you must have a blank image on the form (imgBlank in this case) that is invisible Using the LoadPicture function with a blank file name is a slightly better way to blank an imageno other image is needed on the form.Using the LoadPicture function with a blank file name is a slightly better way to blank an imageno other image is needed on the form.
9. The Toybox Program Set the DragDrop property of each control on the formany control might receive the source control
Set the DragIcon property of each source object
Set the DragOver property of the form
10. Hands-On Example (mailbox) Set DragDrop, DragIcon, and DragOver events for each object on a form
Letter can be sent to mailbox or trash The form as it appears when the project begins execution:
The form as it appears when the project begins execution: