Visual Basic.Net Drag & Drop Tutorial
1.1 Simple definition for drag and drop
1.2 How drag n drop works
1.3 Properties and methods of drag and drop
1.4 System requirements
- Drag & Drop operation – simple example
2.1 Simple example for drag & drop type operation
2.2 Downloadable zip file
- VB.net Drag and drop type operation – another example
3.1 Explains how example2 is different from example1
3.2 Downloadable zip file
- An advanced use of Drag-n-drop in VB.net application with example
4.1 Brief description
4.2 Downloadable zip file
1.1. Simple definition for drag & drop
In computer graphical user interface drag & drop is the action of clicking on an object (virtual object on the screen) and dragging it
to different location (on screen) as required.
The basic sequence involved in drag & drop is
- Press and hold down the button on the mouse or the other pointing device to “grab” the object.
- Drag” the object/cursor/pointing device to the desired location.
- “Drop”the object by releasing the button.
A simple drag & drop sequence is given below.
Fig1 contains 2 columns: first column contains animal names and second column contains small boxes where relevant images are dropped.
Fig 1: Images before being dragged to the target
Drag and drop the relevant images
Fig2. Images after being dragged to the target
1.2. How Drag and Drop works
Drag and drop is actually the same as cutting and pasting (or copying and pasting) using the mouse instead of keyboard. In both cases you have
source (where you are cutting or copying from) and a target (where you are pasting to). During either operation, a copy of the data is
maintained in memory. Cut and paste uses clipboard; drag and drop uses a data object, which is in essence private clipboard.
1.3. Properties and methods of Drag & drop
Here is the sequence of events in drag and drop operation:
- Dragging is initiated by calling DoDragDrop method for source control.
The DoDragDrop method takes 2 parameters
- Data, specifying the data to pass
- Allowed effects, specifying which operations (copying and/or moving) are allowed
A new data object is automatically created.
1.4. System Requirement
- This in turn raises GiveFeedbackevent. In most cases you do not need to worry about Givefeedback event,but if you wanted to display a custom mouse pointer during the
drag, this is where you would add your code.
- Any control with its AllowDropproperty is set to true is potential drop target. AllowDrop property can be set at design time or programmatically at Form Load
- As the mouse passes over each control, the DragEnterevent fro that control is raised. GetDatPresentmethod is used to make sure that format of data is appropriate to the
target control and Effect property is used to display the appropriate mouse pointer.
- If the user releases the mouse button over a valid drop target, the DragDrop event is raised. Code in the DragDrop event handler extracts
the data from the DataObjectobject and displays it in the target control.
The example code given in the tutorial runs on any Windows computer running windows7, XP, vista. Project is done in Visual Basic. Net. VB
programmers primarily intend the tutorial for use, and you may be requiring VB.net software installed on your computer to run the program.