Now we’ll see how to drag editable controls (such as TextBox controls) without sacrificing the control’s editing features. To initiate a drag operation manually, call the Drag method. Calling the Drag method for a control is equivalent to set- . ting its DragMode property to I’-Automatic for as long as the control is being dragged. After the control is dropped somewhere on the Form, the DragMode
property is reset. The DragMode property doesn’t change, but the control. behaves temporarily as if it is set to I-Automatic .
When do you call the Drag method of a control? First, you must decide how the application will differentiate between regular editing operations with the mouse and dragging operations. One safe approach is to use the Control or Alt key with the mouse to drag the control. All the functions of the mouse are available as usual. If the mouse button is pressed while the Control key is down, however, the code initiates a drag operation by calling the control’s Drag method. Or, you can use the right mouse click to initiate a drag-and-drop operation.
VB6 at Work: The TextDrop Project
Let’s see how manual dragging is done. Figure 4.16 shows a Form with two TextBox controls and a Label control. The project is called TextDrop and can be found in this chapter’s folder on the CD.
The two textboxes have their DragMode property set to Automatic and Manual, as indicated by their initial captions. Both TextBox controls can be dropped on the Label control. Let’s start by looking at the code behind the Label control’! DragDrop event
The label Control’s DragDrop Even
This subroutine displays a message with the name of the control that is dropped on the label. There is no need to test for the type of the control, since all control! have a Name property.
The first control can be dropped automatically. This means that you can edit t contents of the first textbox using only the keyboard. To select a range of text a the control, you hold down the Shift key and press the arrow keys. If you drag tI mouse on the first textbox, it will start moving. Drop it on the Label control an watch the label’s caption change to reflect the name of the control that is dropped The second textbox can be edited as usual. To drag it, you must click the continue while holding down the Control key and then start dragging it. The Drag meth of the control is called from within the MouseDown event if the Control button pressed, as shown in Code 4.16.
The Second TextBox Control’s MouseDown Event
Run the TextDrop application and see how it behaves, The’ Drag method is quite simple and will become your tool for adding drag-and-drop features to editable controls. You should also use the Drag method with noneditable controls, such as labels or Command buttons, which should also react to the common mouse events.
Dragging List Items
Another problem with drag-and-drop operations is that while you’re dragging a large control, Visual Basic moves the control’s outline around. If the source control is a ListBox control, there’s no need to drag the entire control; drag only the selected element. The trick is to drag a smaller control, such as a Label control, whose dimensions are the same as the item’s dimensions. In the last section, we’re going to look at this technique with an example
VB6 at Work: The ListDrop Project
The application shown in Figure 4.17 is called ListDrop, and you’ll find it in the ListDrop folder on the CD. The operation of this application W’lS explained in the section “Drag-and-Drop Operations,” earlier in this chapter
The Form contains all the controls you see on it plus an invisible Label control. When the user clicks the mouse over a ListBox control, the invisible Label control (called DragLabel) is resized to the width of the corresponding ListBox control and to the height of a line in the list. It is then repositioned over the selected item in the list. Its Drag method is called to initiate a drag.operation, from within the ListBox control’s MouseDown event, which is shown next.
The FileListBox Control’s MouseDown Event Handler
The ListBox control can’t use the Source argument to find out which control is, dropped on it. It’s always the label that’s being dropped. The control in which the drag operation is initiated remains active to the end of the drag-and-drop operation, so the TEMPList control can access the ActiveControl property of the Form to figure out where the item is coming from. Since the TEMPList and TAPEList controls can only accept data from the FileList control, in their DragDrop event handler they must examine the ActiveControl’s Name property. Hit’s FileList (the name of the FileListBt>xcontrol), the selected item must be added to the corresponding list’s contents. The code of the TAPELisCDragDrop event handler is identical. when the user selects an item in the TEMPList and TAPE Listcontrols to remove, the code in Code 4.18 is executed
The TEMPList Control’s DragDrop Event Handler
Again, a drag operation is initiated.
The, item is removed from the corresponding list if it’s dropped on the FileList control. When the item is dropped on the FileList control, it signifies the user’s intention to remove the me from the corresponding list. In the control’s Draglzrop event we must first examine the control from which the file name was dragged. Again, we can’t use the Source argument of the DragDrop event; we must use the ActiveControl object; The FileList control’s DragDrop event is shown next.