Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
12Activity
0 of .
Results for:
No results containing your search query
P. 1
Drag and Drop in Visual Basic

Drag and Drop in Visual Basic

Ratings: (0)|Views: 1,065|Likes:
Published by jmm744
Code for Visual Basic 6.0
Code for Visual Basic 6.0

More info:

Published by: jmm744 on Mar 16, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

11/18/2012

pdf

text

original

 
 http://www.devx.comPrinted fromhttp://www.devx.com/vb/Article/8029/1954 
Implementing Drag-and-Drop in Visual Basic 6
The cross-program similarities in Windows operating systems' user interface commands makelife 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 atraditional drag-and-drop interface is a simple task in Visual Basic 6. This article shows youhow to do it.
by Peter G. Aitken
ne major advantage of the Windows operating system—and most other operating systems for thatmatter—is that it provides all programs with a similar user interface. A program's menu, for example, is alwaysdisplayed across the top of its window, and its commands can be accessed with the mouse or by using the Altkey. Pressing Alt+F4 closes a program, F1 is almost always the Help key, and Ctrl+P usually is the commandfor printing. These and other cross-program similarities in user interface commands make the user's life a loteasier. Imagine having to learn a new interface and all new commands for each program!One important aspect of the Windows interface that many developers overlook is drag-and-drop. The ability tocarry out program operations using drag-and-drop can be a significant part of a user interface, particularly for those users who are happier using the mouse rather than the keyboard. Implementing drag-and-drop in aVisual Basic 6 program is a relatively simple task. This article covers only traditional drag-and-drop, whichpermits items to be dragged from one part of a program to another part of the same program. There is asecond kind of drag-and-drop that permits items to be dragged from one program to another program, whichis not covered here.
The Basics of Drag and Drop
 A drag-and-drop operation involves a source and a target. The source can be any Visual Basic control, andthe target can be any control or the form itself. The operation has three parts:
The operation begins when the user presses the mouse button as the pointer is over a control that hasbeen 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 DragOver 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 DragDropevent that it has been dropped on.It's important to note that what is being dragged is the source control. It's not being dragged literally—it doesnot move on the form—but its identity is being dragged. Both the DragOver and DragDrop events inform thetarget control of the identity of the source control, and code in the event procedure can take actionaccordingly.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. The control can act as a drag-and-drop sourceonly if you put the required code in the MouseDown event procedure to begin the operation. Set tovbAutomatic (value = 1) to have drag-and-drop initiated automatically when the user depresses the
 
Página 1 de 4Implementing Drag-and-Drop in Visual Basic 601/02/2009http://www.devx.com/vb/Article/8029/1954
 
mouse on the control.
DragIcon. Specifies the mouse pointer that is displayed while the control is being dragged. The defaultsetting displays an arrow with a rectangle. For a custom mouse icon, set this property at design-time toa .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 the its DragMode property is set tovbManual. 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 sourcecontrol'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 isdragged 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 anadditional 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
give the horizontal and vertical position of the mouse pointer with respect to
object 
. These values arealways 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:1.When the mouse pointer leaves the source control, the parent form receives a single DragOver eventwith the State argument equal to 0.2.As the pointer moves over the form the form receives multiple DragOver events with the Stateargument equal to 2.3.When the source is dragged over another control on the form, the form receives a DragOver event withthe State argument equal to 1 (signaling that the pointer has left the form), and the control over whichthe source was just dragged receives a DragOver event with the State argument equal to 0 (signalingthat the pointer has entered the Form).4.When the control is dropped, the object it is currently over receives a DragDrop event.Let's look at some examples, starting with something really simple. Create a Visual Basic project and place aText Box and a Label on the form. Set the Text Box's DragMode property to vbAutomatic. Put the followingcode in the Label's DragDrop event procedure:
Private Sub Label1_DragDrop(Source As Control, _X As Single, Y As Single)Label1.Caption = Source.TextEnd Sub
When you run the project, enter some text in the Text Box then drag from the Text Box to the Label. Doing
Página 2 de 4Implementing Drag-and-Drop in Visual Basic 601/02/2009http://www.devx.com/vb/Article/8029/1954
 
this, you'll see that the text is copied from the Text Box to the Label.Now add some enhancements. Suppose you do not want the drag operation to be possible if the Text Box isempty. Change the Text Box's DragMode property back to the default setting of vbManual. Then, add thiscode to the Text Box's MouseDown event procedure:
Private Sub Text1_MouseDown(Button As Integer, _Shift As Integer, X As Single, Y As Single)If Len(Text1.Text) > 0 And Button = 1 ThenText1.Drag vbBeginDragEnd IfEnd Sub
The If statement checks to see if the Text Box contains text and also makes sure that the left mouse button isdepressed, as is traditional for drag-and-drop operations. Only if both conditions are met is a drag-dropoperation started.In the previous example, the mouse cursor displayed its default dragging cursor while the Text Box was beingdragged—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 theform. To do this, use an icon editor to create two icons, one a red circle with a slash through it named NO.ICOand 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 icondisplayed during dragging will be the "no" icon unless it is explicitly modified. Next, write code to change theicon to YES.ICO when the cursor is dragged over the Label control, and to change it back to NO.ICO whenand 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 ThenText1.DragIcon = LoadPicture(App.Path & "\yes.ico")End IfEnd SubPrivate Sub Form_DragOver(Source As Control, _X As Single, Y As Single, State As Integer)If Source.Name = "Text1" And State = 0 ThenText1.DragIcon = LoadPicture(App.Path & "\no.ico")End IfEnd Sub
Now, the cursor displays the "no" symbol, as shown inFigure 1, whendragging over the form. Only when the cursor is over the Label controldoes the cursor display the "yes" icon (Figure 2).Sometimes your drag-dropcode will be interested in thetype of the source rather thanin its specific identify. For example, the demonstrationprogram could be modified tocontain multiple Text Boxcontrols, and you want toenable drag-and-drop fromany of them to the Labelcontrol. Then you can use theTypeOf operator to determinethe type of the source control. For example:
If TypeOf Source Is TextBox Then
Figure 1 | Click hereto get a close-up view of the cursor displaying the"no" icon.Figure 2 | Click hereto get a close-up view of the cursor displaying the"yes" icon.
 
Página 3 de 4Implementing Drag-and-Drop in Visual Basic 601/02/2009http://www.devx.com/vb/Article/8029/1954

Activity (12)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Inderjit Sharma liked this
Anil Kumar liked this
Del "Abe" Jones liked this
octaviancojocaru liked this
Vaibhav Saxena liked this
Ramy Haddad liked this
Ramy Haddad liked this
gagandeepmnnit liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->