Drag and Drop Conversion Steps
This section will describe how to implement the Drag and Drop events manually.
Description
Drag and drop are actually the same as copy & paste using the mouse instead of the keyboard. In both cases, you have source control (where you are cutting or copying from) and a target control (where you are pasting to). During either operation, a copy of the data is maintained in memory. Cut and paste uses the Clipboard; drag and drop uses a DataObject object, which is in essence a private clipboard.
VB6 supports a lot of drag and drop types: Standard and OLE drag and drop with manual and automatic modes for both types.
Standard Drag and Drop
Data is passed by using the source control as a drag and drop parameter. The drag operation sets the source control in drag and drop mode. DragOver and DragDrop event handlers in the target control receive the source control so they can obtain the data to be copied or moved from the source control itself.
OLE Drag and Drop
Data is passed by using the source control as a drag and drop parameter. The data to be copied or moved is set at the beginning of the drag and drop operation inside the OLEStartDrag event handler or by demand by the OLESetData event handler. The OLEDragOver and OLEDragDrop event handlers received the data in a DataObject parameter
.NET drag and drop
Drag and Drop in .NET is very similar to VB6 manual OLE drag and drop. The concept and functionality are the same but the names and sequences of events are different.
Handling of the DragOver event is very different in .NET. VB6 provided one handler with the action specified by a state parameter. In .NET the handling is now realized by three different event handlers, one handler for each possible DragOver “states”: drag enter, drag over and drag leave.
Programming of the drag and drop feedback to the user has changed as well. In VB6, the visual notification of allowed drag and drop operations had to be programmed in the DragOver event, where the mouse icon could be changed to another to indicate that the control allows drag and drop operations. The code in the DragDrop event was the one that managed what to do with the data dropped into the control. In .NET, the controls that allow drag and drop events need to have that feature enabled first. The feature is enabled by setting the AllowDrop property of the control to true. Also, the types of allowed drag and drop operations must be specified by setting a DragDropEffect value in the drag over or drag enter event handlers.
.NET now handles automatically the changes in mouse icons during drag and drop operations. If the programmer desires to handle mouse pointer icons manually, the procedure for doing it is somewhat different than in VB6. In VB6’s standard drag and drop, the mouse pointer changes had to be specified in the event handlers of each one of the target controls. In .NET, handling this functionality can be simplified by handling the mouse pointer changes in the GiveFeedback event handler of the source control.
Differences between VB6 standard drag and drop and .NET drag and drop.
Functionality
Visual Basic 6.0
.NET
Start Drag method
object.Drag
object.DoDragDrop
Drag Drop event handler
object_DragDrop
object_DragDrop
Drag Over event handler
object_DragOver
object_DragEnter
object_DragOver
object_DragLeave
Drag Over states
state parameter of DragOver event handler
object_DragEnter
object_DragOver
object_DragLeave
Drag icon property
object.DragIcon in each control’s DragOver event handler
1. GiveFeedbackEventArgs parameter in drag over event handler (auto)
2. object.Cursor property of target control (custom)
Differences between VB6 OLE drag and drop and .NET drag and drop.
Functionality
Visual Basic 6.0
.NET
Start Drag method
object.OLEDrag
object.DoDragDrop
Start Drag event handler
object_OLEStartDrag
N/A (use DoDragDrop method)
Drag Drop event handler
object_OLEDragDrop
object_DragDrop
Drag Over event handler
object_OLEDragOver
object_DragEnter
object_DragOver
object_DragLeave
Feedback event handler
object_OLEGiveFeedback
object_GiveFeedback
Drag complete handler
object_OLECompleteDrag
object_DragDrop (target)
Set Data event handler
object_OLESetData
N/A ( use DragEventArgs Data.SetData method in DragDrop event handler)
Conversion of Standard Drag and Drop
Drag method
Calling the Drag method starts the drag and drop operation.
Source code
The method that starts the drag and drop action is normally located inside an event handler, for example, the mouse click, mouse down or mouse move event handlers.
Expected code
Insert the following code. An instance of DraggedObject class will be created to enclose the instance of the control from which the data will be dragged.
VB6
C#
Required support class
The DraggedObject class will be used to pass the instance of the control used as a source for the drag and drop.
C#
VB.NET
DragOver event
The DragOver event is received by a target control when the mouse pointer passes over the control during a drag and drop operation.
In .NET, all controls require to have the AllowDrop property set to True in the designer code section to be capable of handling of DragDrop and DragOver events.
It’s also very important to note that every control which receives DragDrop actions has to allow the drag and drop operation first. The types of allowed drag and drop operations are set with a DragDropEffect value in the drag over or drag enter event handlers. As a default for this spec, the DragDropEffect value will be set in the DragEnter event handler.
If the original code didn’t have code to handle the drag over events, the conversion process has to insert a DragEnter event handler to allow the drag and drop.
Source code
Expected code
First of all, add the following line of code to the designer code for the control. This allows the control to receive drag and drop events.
C#
VB.NET
Leave the original DragOver handler’s parameters. Rename the event handler to control_DragOverEvent to avoid name conflicts with control’s .NET event handlers. Note the required conversion of .NET event’s screen coordinates to client control coordinates, which is done by the ConvertCoordinatesToClient support method.
C#
VB.NET
Generate the drag enter, over and leave event handlers for the Control which is receiving the drag and drop event. Insert calls to the above method in the generated event handlers, adding the expected parameters.
Also, add the line required to allow the Drag and Drop operations in the DragEnter event handler.
C#
C# requires registering the handlers in the designer code
VB.NET
Required support methods
Add the following support methods to the project’s Support class. GetSourceObject method is used to extract the instance of the source control of the drag and drop process. ConvertCoordinatesToClient method is used to convert the screen coordinates produced by .NET events to coordinates inside the control which is receiving the event
C#
VB.NET
Additional code for controls with no DragOver handlers in source
When converting controls without DragOver handling in source code, the conversion tool has to add code to specify that the control can receive drag and drop operations.
Add a .NET DragEnter event handler and add the following line of code:
C#
C# requires to register the event in the designer code
VB.NET
DragDrop event
This event is raised at the time the user releases the mouse button over a target, after a drag and drop operation.
Note that .NET controls require to have the AllowDrop property set to True, to be capable of handling of DragDrop and DragOver events.
Source code
Expected code
Insert a .NET DragDrop event handler for the control. Add code to support the parameters missed during the conversion: ‘Source’, ‘x’, and ‘y’. Note that the event coordinates have to be converted to client coordinates since .NET event gives the mouse coordinate values relative to the screen.
C#
VB.NET
DragIcon property
DragIcon property of controls can be used to change the mouse pointer during a drag and drop operation.
Source code
Expected code
C#
VB.NET
DragMode property
DragMode property specifies how drag and drop operations will be handled: Automatic uses windows drag and drop behavior and Manual uses the behavior coded by the programmer.
Source code
Automatic drag mode allows the whole control to be dragged. This feature is not supported in .NET.
VB6 Manual drag mode requires coding all the drag and drop handling. This is the same as .NET drag and drop.
Expected code
Automatic drag and drop is not supported in .NET
C#
VB.NET
Manual drag and mode is the default behavior of .NET drag and drop. All drag and drop handling must be coded.
C#
VB.NET
Conversion of manual OLE Drag and Drop
VB6 OLE Drag and Drop is very different from VB6 Standard Drag and Drop. OLE Drag and Drop has more events and handlers to enhance the functionality, so its conversion to .NET will be very different as well.
OLEDrag method
VB6 manual OLE Drag and Drop starts with the object.OLEDrag method. However, this Drag and Drop variant requires setting the Drag data and the allowed drag methods inside the OLEStartDrag event handler. To convert the original OLE Drag and Drop behavior to .NET, the OLEDrag and OLEStartDrag members will be converted together.
Source code
Expected code
The call to OLEDrag method will be replaced by the converted OLEStartDrag member. The settings for the drag and drop action will be defined inside that method.
C#
VB.NET
OLEStartDrag
This event was raised in the source control at the start of the drag and drop operation caused by a call to the OLEDrag method of the control. (See OLEDrag method)
Source code
Expected code
Data and AllowedEffects parameters are no longer necessary in the converted method header since the data and the allowed modes are passed in the call to the control’s DoDragDrop method.
Convert the Data.SetData and AllowedEffects assignations into one call to DoDragDrop method.
The call to DoDragDrop method returns the drag and drop effect accepted when the operation ends. It will be stored to support the conversion of other VB6 OLE drag and drop events.
C#
VB.NET
OLEDragOver event
The DragOver event is received by a target control when the mouse pointer passes over the control during a drag and drop operation.
.NET controls require having the AllowDrop property set to True in the designer code section to be capable of handling of DragDrop and DragOver events.
It’s also very important to note that every control which receives DragDrop actions has to allow the drag and drop operation first. The types of allowed drag and drop operations are set with a DragDropEffect value in the drag over or drag enter event handlers. As a default for this spec, the DragDropEffect value will be set in the DragEnter event handler.
If the original code didn’t have code to handle the drag over events, the conversion process has to insert a DragEnter event handler to allow the drag and drop.
Source code
Expected code
First of all, add the following line of code to the designer code for the control. This allows the control to receive drag and drop events.
C#
VB.NET
Remove all the original parameters but State. Insert a new DragEventArgs parameter as the first parameter. Rename the event handler to control_DragOverEvent to avoid name conflicts with control’s .NET event handlers.
Note the required conversion of .NET event’s screen coordinates to client control coordinates, using the ConvertCoordinatesToClient support method.
C#
VB.NET
Generate the drag enter, over and leave event handlers for the Control which is receiving the drag and drop event. Insert calls to the above method in the generated event handlers, adding the expected parameters.
Also, add the line required to allow the Drag and Drop operations in the DragEnter event handler.
C#
VB.NET
Required support methods
Add the following support methods to the project’s Support class. GetSourceObject method is used to extract the instance of the source control of the drag and drop process. ConvertCoordinatesToClient method is used to convert the screen coordinates produced by .NET events to coordinates inside the control which is receiving the event
C#
VB.NET
Additional code for controls with no DragOver handlers in source
Add a .NET DragEnter event handler and add the following line of code
C#
C# requires to register the event in the designer code
VB.NET
OLEDragDrop event
This event is raised at the time the user releases the mouse button over a target, after a drag and drop operation.
Source code
Expected code
The DataObject, Effect, Button, X, and Y coordinates parameters are now contained by the DragEventArgs parameter. The conversion process should insert additional code inside the event handler to extract the required values being used by the user’s code. Conversion code for parameters not used in the user’s code can be omitted.
C#
VB.NET
Parameters mappings:
VB6 Reference
.NET Reference
Data (DataObject)
e.Data (DataObject inside e event arguments)
Effect (Long)
e.Effect (DragDropEffects)
Button
e.KeyState masked with hex 100000
Shift
Control.ModifierKeys masked with hex 10000
X
e.X (needs to be converted to control coordinates)
Y
e.Y (needs to be converted to control coordinates)
OLECompleteDrag
This event was raised to notify the source control when the drag-and-drop operation was complete. This was typically used to remove the data from the source for a cut and paste operation.
Source code
Expected code
C#
VB.NET
Insert a call to the OLECompleteDrag method inside the OLEStartDrag method, just after the call to DoDragDrop method. Pass the Drag and Drop effect value returned by the DoDragDrop call.
C#
VB.NET
OLEGiveFeedback
This event is raised whenever the mouse passes over a possible target, during a drag and drop operation.
Source code
Expected code
To convert the GiveFeedback event handler, generate a .NET GiveFeedback event handler. References to Effect parameter must be changed to e.Effect and references to DefaultCursors parameter to e.UseDefaultCursors, with ‘e’ being the GiveFeedbackEventArgs parameter.
C#
VB.NET
OLESetData
OLESetData event is fired when the drag and drop operation ends and the Data was not set in the OLEStartDrag event at the start of the drag and drop operation. This helps to delay the setting of the Data to whenever the programmer considers it more appropriate.
Conversion and support for this event require modifying the code inside the OLEStartDrag converted event, in order to pass some indicator of data not set yet to the target control’s DragDrop event handler.
Source code
Note the empty parameter in the SetData method inside the OLEStartDrag event handler. It indicates that the Data will be set inside the OLESetData event handler when another control asks for it calling the DataObject GetData method.
Note the empty value parameter in the Data.SetData method call.
Expected code
Assuming that the delayed data set is not widely used, the conversion process can use a simpler method to recover the functionality. Basically, the delayed setting feature will be removed and the data will be set since the start of the Drag and Drop operation.
It’s possible that in the OLESetData event handler, the Data to be set can change depending on the DataFormat value. This DataFormat is set in the GetData method at the end of the drag and drop operation, by the target control.
The best compromise is to assign the value corresponding to the same format set to the data object inside the OLEStartDrag event handler.
I.e., in the source code, the data format set at the beginning (OLEStartDrag) is vbCFText, and the data parameter is empty. Later (in OLESetData), the data assignation will depend on the DataFormat parameter that could be vbCFText or vbCFRTF. The conversion process will choose the assignation corresponding to vbCFText (from OLEStartDrag) which is “SetData Text, vbCFText”, so the value to be set to the data object will be “Text”
The OLESetData method should be commented out, in case that the programmer needs to check anything of the original behavior of the code.
C#
VB.NET
Conversion of constants for OLE drag and drop
VB6 format constants can be converted to System.Windows.Forms.DataFormats constants.
Equivalences of format constants.
VB6 Constant
.NET Constant
vbCFText
DataFormats.Text
vbCFBitmap
DataFormats.Bitmap
vbCFMetafile
DataFormats.MetafilePict
vbCFEMetafile
DataFormats.EnhancedMetafile
vbCFDIB
DataFormats.Dib
vbCFPalette
DataFormats.Palette
vbCFFiles
DataFormats.FileDrop
vbCFRTF
DataFormats.Rtf
VB6 drag and drop constants can be converted to System.Windows.Forms.DragDropEffects constants.
Equivalences for allowed drag and drop effects constants
VB6 Constant
.NET Constant
vbDropEffectNone
DragDropEffects.None
vbDropEffectCopy
DragDropEffects.Copy
vbDropEffectMove
DragDropEffects.Move
vbDropEffectScroll
DragDropEffects.Scroll
Additional mappings to support conversion
VB6 drag and drop uses a DataObject object to pass all the data and information used by the drag and drop operation as a parameter to all events. .NET uses a similar approach but the DataObject object is encapsulated inside the DragEventArgs parameter of the events.
VB6 Member
.NET Member
DataObject.GetFormat(int)
DataObject.GetDataPresent(DataFormats)
DataObject.SetData(p1:object,p2:int)
DataObject.SetData(p2:DataFormats, p1:object)*
DataObject.GetData(int)
DataObject.GetData(DataFormats)
* Conversion of DataObject.SetData needs a special conversion when starting drag and drop action as described in section OLEStartDrag.
DragMode property
Source code
Automatic drag mode allows the whole control or its contents to be dragged. This feature is not supported in .NET.
VB6 Manual drag mode requires coding all the drag and drop handling. This is the same as .NET drag and drop.
Expected code
Automatic drag and drop is not supported in .NET
C#
VB.NET
Manual drag and mode is the default behavior of .NET drag and drop. All drag and drop handling must be coded.
C#
VB.NET
Last updated