The DragMode Property

Nearly all controls have a DragMode property, which determines whether a control can be dragged with the mouse. The DragMode property can have one of the following settings;
• O-Manual Drag operations must be initiated from within the code.
• Automatic The user can drag the control with th.e mouse.

When you set a control’s DragMode property to Automatic, Visual Basic displays the control’s outline as the user moves the control around and notifies the controls that happen to be underneath the control as it’s dragged. Visual Basic doesn’t perform any actions, such as copying one control’s text onto another, This is the programmer’s responsibility. To implement drag-and-drop features in your applications, you must firSt decide which controls can be dragged and on which control they can be dropped. The control being draggedis the source control. All other controls on the Form are the destinations of the drag-and-drop operation. To initiate a drag-and-dropoperation, you must either set a control’s DragMode property to Automatic or call the control’s Drag method. The DragMode=Automatic setting and the Drag method are identical, but the Drag method gives you more control over the operation. For example, you-may not initiate a drag operation if the source control is empty.

The DragDrop and DragOver Methods

A drag operation ends with a drop operation; when the user releases the mouse button. By default, controls don’t react to the drop operation. To make a control react when another control is dropped on it, you must supply some code in its Drag Drop event, which is defined as follows:

“Sub control_DragDrop(Source As Control, X As Single, Y As Single) The control item can be any control on the Form. Source is an object that represents the control that w~s dropped. It’s not a control’s name or other property; it’s an object variable. You can use it to access the various properties of the control that was dropped. X and Yare the coordinates of the mouse at the moment the source control was dropped on the destination control.

In addition to reacting to the dropping of a source control, the destination control can react to the movement of a control being dragged over it. This condition is detected with the DragOver event, which is generated as long as the source control is dragged over the destination control. The definition of the DragOver event is its follows:

Source is the control being dragged.X and Yare the current coordinates of the mouse, and the State argument corresponds to the transition state of the centre being dragged. It can have one of the following values:

• 0 The source control enters the target’s area.
• 1 The source control leaves the target’s area.
• 2 The source control moves over the target’s area.

Depending on whether the control under the pointer can accept the drop operation or not, use the OragOver event to prepare the destination control for the drop operation, or change the mouse pointer to a different icon. VB6 at Work: The DrpEvnts Projed To see these drag-and-drop events in’actions, let’s design the application show= in Figure 4.13. The application is called DrpEvnts, and you will find it in the chapter’s folder on the CD.

The application will monitor the dragging of the Command button and react as follows.
• When the button is first dragged over the picture box, the picture box is painted red.
• When the button leaves the picture box, the picture box is painted green.
• If the user drops the button while it’s over the picture box, the picture box is painted blue . ..

To design the application, follow these steps:

1. Start a new project and place a picture box and a Command button on the Form.
2. Set the PictureBox control’s Background property to a green color, and set the Command button’s OragMode property to l-Automatic.
3. Enter the following code in the PictureBox control’s DragOver event handler:

4. Enter !he following code in the control’s DragDrop event

Run the application and see how it monitors the movement of the mouse. The State ar&ument of the OragOver event takes the value 0 only when the button is dragged over the PictureBox control for the first time. After that, the DragOver event is triggered as the button is moved over the picture box, but the State argument is 2. Finally, as the button is moved outside the area of the PictureBox control, the DragO.ler event is triggered one last time, and this time, its State argument
Notice that the code of this application is concentrated in the two drag-related events of the Picture Box control and that you don’t have to supply any code for the Command button’s events. Simply setting its DragMode property to Automatic was enough to add the dragging capability to the control. Moreover, the code doesn’t use the Source argument of the two events. The Picture Box control would react exactly the same no matter which control you dragged over or dropped on it.

The lYpe Of Keyword

The Draglzrop and DragOver events’ Source argument represents an object so that the destination control can figure out which control was dropped on it. To access the name of the control that was dropped, use the following expression: Source. Name Suppose you want to read the Text property of TextBox controls and the Caption property of Label controls when they are dropped on the destination control. Normally, you would use the following expressions to retrieve these two properties:

Source. Text

Source.Caption

But you have to know whether the dropped control was a TextBox control or a Label control. If not, your code may attempt to access the Label’s Text property or the TextBox’s Caption property and crash with a runtime error, To find out the type of object dropped, use the TypeD! statement, which has the following syntax:

TypeOf objectname Is object type

The objectname item is an object, and objecttype is an object type. To find out whether the source control is a TextBox control, use an If structure such as the following:

If TypeOf Source ‘Is Textbox Then

MsgBox Source. Text

End If . “

You can use the TypeO/keyword anywhere in a Visual Basic application, but it’s commonly used in drag-and-drop operations. We’ll use this keyword later in our applications to differentiate among the various types of controls being dropped.

Mouse Conflicts

Let’s experiment a little with dragging operations. We’ll add a line of code to the Command button’s Click event to make the button react to the mouse click by displaying a message in the Immediate window with the following statement:

Debug.Print ‘I was clicked’

Run the application and click the Drag-and-Drop button with the mouse. No . message will appear in the Immediate window. When Visual Basic starts a dragand- drop operation, it doesn’t generate the usual mouse events (Click or Mouse- Down). If you reset the button’s DragMode property to 0 (Manual), Visual Basic will Start responding to the mouse events on the button, but it won’t drag it

To make matters even worse, add a TextBox control on the Form, set its Drag- Mode property to Automatic, and then run the application again ..You can enter text. in the control, move the control around, but the editing operations of the mouse are ‘gone. If you attempt to select some text with the mouse, a drag operation starts. The editing features of the mouse are taken over by the dragging operations. Our experiments indicate that automatic dragging must be designed carefully. You can’t simply set a control’s DragMode fo Automatic and expect it to work as before. If the control is a Label, you don’t have to worry about editing it. Setting its DragMode property to Automatic isn’t going to cause any serious problems, except that you won’t be able to use the ~ommon mouse events. If you don’t need the Click event handler of the Label control in your application, you can set the control’s DragMode to Automatic and implement drag-and-drop features.If you set a Command button’s DragMode to Automatic, you can no longer count on the ‘”ntrol’s Click event. The solution to these conflicts is to implement drag-and-drop features manually. You’ll see how this is done, but first let’s lock at a couple of simpler applications that demonstrate typical uses of drag-and-drop operations. VB6 at Work:

The DragDrop Projed

The DragDrop application, shown in Figure 4.14, consists of a single Form that contains a Label, TextBox, and Picture Box controls ..The Label and TextBox controls’ OragMode property is set to I-Automatic, so you can drag them on any . other control. Run the application, enter some text in the textbox (without using the mouse’s editing features, just the keyboard), and then drop the TextBox control on the Label.

If you enter the full path name of an image file in the textbox and then drop the control on the Label control, the filename is copied to the Label control. If you drop it on the picture box, the image is displayed there. These are two common uses of drag-and-drop operations.
To make sure we make sure that the control being dropped is the Label control, and to assign the TextBox control’s text to the Label control’s caption, use the listing shown in Code 4.11 and 4.12

The DragDrop Event Handler Code for the TextBox Control.

You may think that we don’t have to test for the object being dropped since the Label is the only object that can be dropped on the TextBox control. That’s not quite true. You can actually drop an object on itself. The DragOrop event handler for the Label control is similar.

The DragOrop Event Handler Code for the Label Control

The Label control reacts to the drop of the TextBox control by copying the text to the Label’ s Caption property.’
The PictureBox control reacts differently to the drop of either control. It assumes that the content of the label or the TextBoxcontrol is the filename of an image, which it attempts to display.

The DragDrop Event Ha.ndler Code tor the Pictur.eBox Control

First; it extracts the iabel’s caption or the textbox’s text (depending on the source control), and then uses it as argument to the LoadPicture method to display the image on the PictureBox control. The error handler is there to prevent runtime errors, should the control contain an invalid filename

VB6 at Work: The DropForm Project

Our next application demonstrates. drag-and-drop operations among multiple Forms. The Drcpf’orm application consists of three Forms. The main Form contains two columns of labels, with state and city names. The other two Forms each contain a ListBox control, and they are initially empty. The user can drag any of the labels on the main Form and drop them on one of the·two lists, which reside on different Forms. .
Design the Forms as shown in Figure 4.15. Place any type of data you want in the Label controls. The Label controls on the first column (which display state names) have their Tag property set to “STATE” and the Label controls on the see- ” and column have their Tag property set to “CITY.” You’ll see later how these tags are used in the code.
. When the main Form is loaded, it must display the other two Forms. Enter the following code in its Load event handler:

The Move method is used to place the smaller Forms to the right of the main Form.

More interesting is the code is in the DragDrop event handlers of the two List Boxcontrols shown next.

The DragDrop Event Hanciler Code of the ListBox Control on the States Form

When a label is dropped on this control, the code of the DragDrop event exalines the source control’s Tag property. If it’s not STATE,then the control can’t 1: dropped and the appropriate message is displayed. If the Tag property is STAlt it sets-the caption of the source control to asterisks to indicate that it can’t be dropped again. Since only Label controls can be dropped on the two ListBox controls, there is no need to test for the type of object being dropped. We use the Source. (apt; on expression directly to access the label’s caption. The DragOrop event handler for the ListBox control on the Cities Form is identical. Only the name of the ListBox control changes (List2 instead of Listl).
Notice that in addition to the caption of the control that is dropped on the list box, we add the name of the Form from which the label comes, in parentheses. The expression ..o..urce .Parent gives you access ‘to the Form to which the source control belongs. Through this expression, you can access the properties of the Form from which the source control came. The expression Source. Parent. Name returns the name of the Form on which the labels with the city and state names are placed.
The drag operation’s destination control doesn’t carewhere the source control comes from. The argument Source has all the’ information it needs to access the source control’s properties, regardless of whether it resides on the same or another Form.

Scroll to Top