|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Thats all it takesnow users can move the text boxes in the dragcontrols example around as they like. The code for this example is located in the dragcontrols folder on this books accompanying CD-ROM. Drag/Drop: Handling DragOver EventsWhen the user drags a control over a form or control, a DragOver event is triggered like this:
Sub Form_DragOver(source As Control, x As Single, y As Single, state As_
Integer)
Here are the arguments this event handler is passed:
Lets see an example. Here, well turn the text boxes in the dragcontrols example that weve developed in the previous few topics blue as the user drags a control over them. To do that, we add a DragOver event to the text boxes in the Textboxes control array:
Private Sub Textboxes_DragOver(Index As Integer, Source As Control, X As _
Single, Y As Single, State As Integer)
End Sub
Here, we simply add code to turn the text box blue when we drag another control over it:
Private Sub Textboxes_DragOver(Index As Integer, Source As Control, X As _
Single, Y As Single, State As Integer)
Textboxes(Index).BackColor = RGB(0, 0, 255)
End Sub
And thats itnow were handling DragOver events, as shown in Figure 28.4.
OLE Drag/Drop: Dragging DataThe Testing Department is on the phone again. A lot of new word processors are allowing users to drag data from application to applicationhow about your new SuperDuperTextPro program? Its not possible, you say. Yes it is, they say, use OLE drag/drop. In OLE drag/drop operations, you can let the user drag data between controls, and even between programs. Heres how it works: when the user presses the mouse button, you start the OLE drag operation with the OLEDrag method (this method has no parameters). This causes an OLEStartDrag event, and you are passed an object of type DataObject in that events handler. You use that objects SetData method to set the data you want the user to drag: DataObject.SetData [ data], [ format] Here, data is a variant that holds the data you want the user to drag, and format indicates the data format, which can be one of these values:
Youre also passed a parameter named AllowedEffects in the OLEStartDrag events handler, and you need to set that parameter to one of the following values:
When the user drops the data onto an appropriate target, which is a form or control with its OLEDropMode property set to Manual (= 1), an OLEDragDrop event occurs, and you are passed a DataObject in that events handler. To get the dragged data, you use the DataObjects GetData method: DataObject.GetData ( format) The format parameter here may be set to the same values as the format parameter for SetData. Lets see an example. In this case, well add two text boxes, Text1 and Text2, to a form, and let the user drag the text from Text1 to Text2. We start at design time by placing the text OLE Drag! into Text1 so the user will have something to drag when the program runs.
When the user presses the mouse button in Text1, then, we start the OLE drag/drop operation with the OLEDrag method:
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As _
Single, Y As Single)
Text1.OLEDrag
End Sub
This triggers an OLEStartDrag event for Text1: Private Sub Text1_OLEStartDrag(Data As DataObject, AllowedEffects As Long) End Sub Here, well let the user drag the text from the text box Text1, and we do that by placing that text into the data object passed to us in the OLEStartDrag event handler:
Private Sub Text1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
Data.SetData Text1.Text, vbCFText
...
End Sub
We also must set the AllowedEffects parameter to the OLE drag/drop operations well allow:
Private Sub Text1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
Data.SetData Text1.Text, vbCFText
AllowedEffects = vbDropEffectMove
End Sub
And thats itnow users can drag the text from the text box. To let them drop that text in the other text box, Text2, well enable that text box for OLE drops in the next topic. OLE Drag/Drop: Dropping DataThe testing department is on the phone again. Its fine that youve allowed users to drag data from controls in your program, but how about letting them drop that data as well? Coming right up, you say.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |