Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Implementing Drag-and-Drop in Visual Basic 6 : Page 4

The cross-program similarities in Windows operating systems' user interface commands make life a lot easier. Drag-and-drop is one such interface, which can be a significant part of a user interface for those who are happier using the mouse rather than the keyboard. Implementing a traditional drag-and-drop interface is a simple task in Visual Basic 6. This article shows you how to do it.


advertisement
In the previous example, the mouse cursor displayed its default dragging cursor while the Text Box was being dragged—the normal arrow plus a rectangle the same size as the source control. There was no indication of where the Text Box could be dropped (such as the Label control) or where it could not be dropped (the form). Change the code so the drag icon indicates whether or not a drop is possible as the cursor moves around the form. To do this, use an icon editor to create two icons, one a red circle with a slash through it named NO.ICO and the other a green checkmark called YES.ICO. Place both icon files in the Visual Basic project folder.

The first step is to modify the DragIcon property of the Text Box to NO.ICO. This means that the default icon displayed during dragging will be the "no" icon unless it is explicitly modified. Next, write code to change the icon to YES.ICO when the cursor is dragged over the Label control, and to change it back to NO.ICO when and if the cursor leaves the Label control and re-enters to Form. Here's the required code:

Private Sub Label1_DragOver(Source As Control, _ X As Single, Y As Single, State As Integer) If Source.Name = "Text1" And State = 0 Then Text1.DragIcon = LoadPicture(App.Path & "\yes.ico") End If End Sub Private Sub Form_DragOver(Source As Control, _ X As Single, Y As Single, State As Integer) If Source.Name = "Text1" And State = 0 Then Text1.DragIcon = LoadPicture(App.Path & "\no.ico") End If End Sub

 
Figure 1 | Click here to get a close-up view of the cursor displaying the "no" icon.



Now, the cursor displays the "no" symbol, as shown in Figure 1, when dragging over the form. Only when the cursor is over the Label control does the cursor display the "yes" icon (Figure 2).

 
Figure 2 | Click here to get a close-up view of the cursor displaying the "yes" icon.

Sometimes your drag-drop code will be interested in the type of the source rather than in its specific identify. For example, the demonstration program could be modified to contain multiple Text Box controls, and you want to enable drag-and-drop from any of them to the Label control. Then you can use the TypeOf operator to determine the type of the source control. For example:

If TypeOf Source Is TextBox Then .... End If

You can modify the demo program with some additional textBox controls. Make the following code modifications so that the proper icon is displayed and the drop operation is performed regardless of which Text Box is the source.

You can modify the demo program with some additional textBox cointrols. Make the following code modifications so that the proper icon is displayed and the drop operation is performed regardless of which Text Box is the source.

Private Sub Form_DragOver(Source As Control, _ X As Single, Y As Single, State As Integer) If TypeOf Source Is TextBox Then Text1.DragIcon = LoadPicture(App.Path & "\no.ico") End If End Sub Private Sub Label1_DragDrop(Source As Control, _ X As Single, Y As Single) If TypeOf Source Is TextBox And State = 0 Then Label1.Caption = Source.Text End Sub Private Sub Label1_DragOver(Source As Control, _ X As Single, Y As Single, State As Integer) If TypeOf Source Is TextBox And State = 0 Then Text1.DragIcon = LoadPicture(App.Path & "\yes.ico") End If End Sub

This demonstration project is available for download as the project DragDrop. It makes a good starting point for your own experiments with Visual Basic drag-and-drop.




Peter G. Aitken has been writing about computers and programming for over 10 years, with some 30 books and hundreds of articles to his credit. Recent book titles include Developing Office Solutions With Office 2000 Components and VBA, Windows Script Host, and the soon to be published XML the Microsoft Way. He is a regular contributor to OfficePro magazine, and for several years was a contributing editor for Visual Developer Magazine where he wrote the popular Visual Basic column. Peter is the proprietor of PGA Consulting, providing custom application and Internet development to business, academia, and government since 1994. You can reach him at peter@pgacon.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date