Home > Software > Drag-n-Drop Using Visual Basic > Introduction

Contents Drag & Drop Operation – a Simple Examples

Visual Basic 6 : Drag-n-Drop Tutorial

Introduction


1.1 What is Drag & Drop

In computer graphical user interface drag & drop is the action of clicking on an object (virtual object on screen) and dragging it to a different location (on screen) as required. 

The basic sequence involved in drag & drop is

  • Press & hold down, the button on the mouse or other pointing device to “grab” the object.
  • “Drag” the object /cursor/ pointing device to the desired location
  • “Drop” the object by releasing the button

The drag-n-drop is programmatically done and it is widely used in creating objective type questions. Both VB6 and VB.NET support a rich variety of drag drop functions as detailed in the following three examples.

Example: 

An example of a simple drag and drop sequence is given below. Fig1 contains two columns: First column contains animal names, and the second column contains small boxes where user needs to drop the relevant images. 

               Animal Name                     Boxes where relevant images are dropped 

Fig 1. Images before being dragged to the target

Fig 2. gives after the images being dragged and dropped to the correct places.

Fig 2. Images after being dragged to the  target

1.2 The Basic Concepts of Drag and Drop

A drag-and-drop operation involves a source object and a target object. The source object can be any Visual Basic control, and the target object can be any control or the form itself.

The operation has three parts:

  • The drag operation begins when the user presses the mouse button as the pointer is over a control that has been enabled as a drag-and-drop source.
  • The operation continues as the pointer is moved, with the left mouse button still down, over other controls or the form itself. A control receives notification, via the Drag Over event, that it is being "dragged over" and can signal whether the data can be dropped on it by changing the appearance of the mouse pointer.
  • The operation ends when the user releases the mouse button. A control is notified by the Drag Drop event that it has been dropped on.

1.3 Properties of Drag & Drop

Controls have two properties that are related to drag-and-drop:

  • DragMode. Set to vbManual (value = 0, the default) for manual drag-and-drop, which requires use of the Drag method to initiate a drag-and-drop operation.
  • DragIcon. Specifies the mouse pointer that is displayed while the control is being dragged. The default setting displays an arrow with a rectangle. For a custom mouse icon, set this property at design-time to a .ICO or .CUR file, or at run-time use the LoadPicture function to load an .ICO file.

Executing the Drag method on the source control is required only when it’s Drag mode property is set to vbManual. The syntax for this method is:

object.Drag action

Set action to vbBeginDrag (value = 1) to initiate a drag operation. This will usually be done in the     source      control's MouseDown event procedure. You can also call the Drag method with action set to vbCancel or vbEndDrag (values 0 and 2 respectively) to cancel an ongoing drag-and-drop operation or to end a drag-and-drop operation.

Controls have two events that are related to drag-and-drop. DragOver is used to detect when an object is dragged over a control, and DragDrop is used to detect when an object is dropped on a control:

Target_DragOver (source As Control, x As Single, y As Single, State As Integer)

target_DragDrop(Source as Control,X as Single,Y as single)

Target identifies the target object (the one being dragged over or dropped on). It can be a form, an MDI    form, or a control. If the target is a control that is part of a control array, these event procedures will have an additional argument that specifies the Index property of the control within the control array.

Source identifies the source control (where the drag-drop operation began).

X and y gives the horizontal and vertical position of the mouse pointer with respect to object. These values are always expressed according to the object's coordinate system

State specifies the relationship between the mouse pointer and the target, as follows:

  • A value of 0 indicates the pointer just entered the target.
  • A value of 1 indicates the pointer is leaving the target.
  • A value of 2 indicates that the pointer is moving within the target.

When a source control is dragged and dropped, here's what happens:

  • When the mouse pointer leaves the source control, the parent form receives a single Drag Over event with the State argument equal to 0.
  • As the pointer moves over the form the form receives multiple Drag over events with the State argument equal to 2.
  • When the source is dragged over another control on the form, the form receives a Drag Over event with the State argument equal to 1 (signaling that the pointer has left the form), and the control over which the source was just dragged receives a DragOver event with the State argument equal to 0 (signaling that the pointer has entered the Form).
  • When the control is dropped, the object it is currently over receives a DragDrop event.

1.4 System Requirements

The example codes given in the tutorial  runs on any Windows computer running XP, 2000, or Vista. Project is done in Visual Basic 6 as front end and Microsoft Access2000 as back end. The tutorial is primarily intended for use by VB programmers, and you may be requiring VB6 software installed on your computer to run the program.

Contents Drag & Drop Operation – a Simple Examples
     Home                                                Copyright © 2003-2016 TutorialsWeb.com                                   Disclaimer                                           Sitemap