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.

theControl.Drag

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

Dim selection As DraggedObject
selection = New DraggedObject()
selection.Data = theControl
theControl.DoDragDrop(selection, DragDropEffects.All)

C#

DraggedObject selection = new DraggedObject();
selection.Data = theControl;
theControl.DoDragDrop(selection, DragDropEffects.All);

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#

namespace Project1
{
    class DraggedObject
    {
        public String DataName; 
        public System.Type DataType;
        public String Value;
        public Object Data;
    }
}

VB.NET

Public Class DraggedObject
    Public DataName As String
    Public DataType As System.Type
    Public Value As String
    Public Data As Object
End Class

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

Private Sub theControl_DragOver(Source As Control, _
    x As Single, y As Single, State As Integer)
    '<code to handle event>
End Sub

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#

theControl.AllowDrop = true;

VB.NET

theControl.AllowDrop = True

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#

private void  theControl_DragOverEvent(Control Source,  
   int X,  int Y,  short State)
{
   Support.ConvertCoordinatesToClient(theControl, ref X, ref Y);
   //Code to handle the event   
}

VB.NET

Private Sub theControl_DragOverEvent(ByRef Source As Control, 
     ByRef e As System.Windows.Forms.DragEventArgs, ByVal x As Single, 
     ByRef y As Single, ByRef State As Short)
Support.ConvertCoordinatesToClient(theControl, x, y)
     '<code to handle the event>
End Sub

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#

private void theControl_DragEnter(object sender, DragEventArgs e)
{
   //UPGRADE_WARNING: The following statement was inserted to allow drag and drop operations
   e.Effect = DragDropEffects.All;

   //UPGRADE_WARNING: The following statement was inserted to allow conversion of drag over event handling
   theControl_DragOverEvent( Support.GetSourceObject(e), e.X, e.Y, 0);
}
private void theControl_DragLeave(object sender, EventArgs e)
{
   //UPGRADE_WARNING: The following statement was inserted to allow conversion of drag over event handling
   theControl_DragOverEvent(Support.GetSourceObject(e), 0, 0, 1);
}

private void theControl_DragOver(object sender, DragEventArgs e)
{
   //UPGRADE_WARNING: The following statement was inserted to allow conversion of drag over event handling
   theControl_DragOverEvent(Support.GetSourceObject(e), e.X, e.Y, 2);
}

C# requires registering the handlers in the designer code

this.theControl.DragEnter += new DragEventHandler(this.theControl_DragEnter);
this.theControl.DragLeave += new EventHandler(this.theControl_DragLeave);
this.theControl.DragOver  += new DragEventHandler(this.theControl_DragOver);

VB.NET

Private Sub theControl_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles theControl.DragEnter

   'UPGRADE_WARNING: The following statement was inserted to allow drag and drop operations
   e.Effect = DragDropEffects.All

   'UPGRADE_WARNING: The following statement was inserted to allow conversion of drag over event handling
   theControl_DragOverEvent(Support.GetSourceObject(e), e, e.X, e.Y, 0)

End Sub

Private Sub theControl_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles theControl.DragOver

   theControl_DragOverEvent(Support.GetSourceObject(e), e, e.X, e.Y, 2)

End Sub

Private Sub theControl_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles theControl.DragLeave

   'UPGRADE_WARNING: The following statement was inserted to allow conversion of drag over event handling
   theControl_DragOverEvent(Support.GetSourceObject(e), e, 0, 0, 1)

End Sub

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#

class Support
{
   /// <summary>
   /// Un-Wraps the control source of the drag and drop data.
   /// </summary>
   /// <param name="e">The DragDrop Event</param>
   /// <returns>Control inside the event</returns>
   public Control GetSourceObject(EventArgs e)
   {
      Object SourceObject = null;
      DragEventArgs dragEvent;
      if (((DragEventArgs)e) is DragEventArgs)
      {
         dragEvent = (DragEventArgs)e;
         if (dragEvent.Data.GetDataPresent
         (typeof(Project1.DraggedObject)))
         {
            SourceObject = ((Project1.DraggedObject)
            dragEvent.Data.GetData
            ("Project1.DraggedObject")).Data;
         }
      }
      return (Control)SourceObject;
   }

   /// <summary>
   /// Convert .NET event's screen coordinates to client control 
   /// coordinates to be used by converted drag and drop members
   /// </summary>
   /// <param name="clientControl">Control to where the coordinates will be traslated</param>
   /// <param name="x">X Coordinate</param>
   /// <param name="y">Y Coordinate</param>
   public void ConvertCoordinatesToClient(Control clientControl, 
ref int x, ref int y)
   { 
      Point  convertedCoordinates = clientControl.PointToClient
(new Point(x,y));
      x = convertedCoordinates.X;
      y = convertedCoordinates.Y;
   }
}

VB.NET

'Un-Wraps the control which is the source of the drag and drop data.
Private Function GetSourceObject(ByVal e As System.EventArgs) As Control
   Dim SourceObject As Object = Nothing
   Dim dragEvent As DragEventArgs
   If TypeOf e Is DragEventArgs Then
      dragEvent = CType(e, DragEventArgs)
      If dragEvent.Data.GetDataPresent("Project1.DraggedObject") Then
         SourceObject = CType( dragEvent.Data.GetData _
                ("Project1.DraggedObject"), _ 
               Project1.DraggedObject).Data
      End If
   End If
   Return SourceObject
End Function

'Convert .NET event's screen coordinates to client control coordinates to be used by converted drag and drop members
Public Sub ConvertCoordinatesToClient(ByVal clientControl As Control, ByRef x As Integer, ByRef y As Integer)
   Dim convertedCoordinates As Point = clientControl.PointToClient(New Point(x, y))
   x = convertedCoordinates.X
   y = convertedCoordinates.Y
End Sub

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#

private void theControl_DragEnter(object sender, DragEventArgs e)
{
   //UPGRADE_WARNING: The following statement was inserted to allow this control to receive drag and drop actions
   e.Effect = DragDropEffects.All;
}

C# requires to register the event in the designer code

this.theControl.DragEnter += new DragEventHandler(this.theControl_DragEnter);

VB.NET

Private Sub theControl_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles theControl.DragEnter

   'UPGRADE_WARNING: The following statement was inserted to allow this control to receive drag and drop actions 
   e.Effect = DragDropEffects.All

End Sub

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

Private Sub theControl_DragDrop(Source As Control, x As Single, y As Single)
    'User's code to handle event
End Sub

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#

private void theControl_DragDrop(object sender, DragEventArgs e)
{
   Object Source;
   int x = e.X;
   int y = e.Y;
   Support.ConvertCoordinatesToClient(theControl, ref x, ref y);
   Source = Support.GetSourceObject(e);
   if (!Source.Equals(null))
   { 

    //Code to handle the event

   }
}

VB.NET

Private Sub theControl_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles theControl.DragDrop
   Dim Source As Object
   Dim x As Single = e.X
   Dim y As Single = e.Y
   ConvertCoordinatesToClient(theControl, x, y)
   Source = Support.GetSourceObject(e)
   If Not Source.Equals(Nothing) Then   

         'Code to handle the event

   End If
End Sub

DragIcon property

DragIcon property of controls can be used to change the mouse pointer during a drag and drop operation.

Source code

theControl.DragIcon = LoadPicture("Cursors\hmove.cur")

Expected code

C#

theControl.Cursor = new Cursor("Cursors\\hnodrop.cur");

VB.NET

theControl.Cursor = New Cursor("Cursors\hnodrop.cur")

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.

theControl.DragMode = vbAutomatic

VB6 Manual drag mode requires coding all the drag and drop handling. This is the same as .NET drag and drop.

theControl.DragMode = vbManual

Expected code

Automatic drag and drop is not supported in .NET

C#

//UPGRADE_WARNING: Control.DragMode was not upgraded.
//theControl.DragMode = vbAutomatic

VB.NET

'UPGRADE_WARNING: Control.DragMode was not upgraded.
'theControl.DragMode = vbAutomatic

Manual drag and mode is the default behavior of .NET drag and drop. All drag and drop handling must be coded.

C#

//UPGRADE_WARNING: Control.DragMode was not upgraded.
//theControl.DragMode = vbManual

VB.NET

'UPGRADE_WARNING: Control.DragMode was not upgraded.
'theControl.DragMode = vbManual

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

theControl.OLEDrag

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#

//UPGRADE_WARNING: The following statement was inserted to allow conversion of drag start event
theControl_OLEStartDrag();

VB.NET

'UPGRADE_WARNING: The following statement was inserted to allow conversion of drag start event
theControl_OLEStartDrag()

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

Private Sub theControl_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
     Data.SetData theControl.SelText, vbCFText
     AllowedEffects = vbDropEffectCopy 
End Sub

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#

private void theControl_OLEStartDrag( )
{ 
   DragDropEffects effect;
   effect = theControl.DoDragDrop(theControl.SelectedText, DragDropEffects.Copy);
}

VB.NET

Private Sub theControl_OLEStartDrag()
   Dim Effect As DragDropEffects
   Effect = theControl.DoDragDrop(theControl.SelectedText, DragDropEffects.Copy)
End Sub

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

Private Sub theControl_OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer)
    'User's code
    If Data.GetFormat(vbCFText) Then
        Effect = vbDropEffectCopy 
    Else
        Effect = vbDropEffectNone
End If
'End of user's code
End Sub

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#

theControl.AllowDrop = true;

VB.NET

theControl.AllowDrop = True

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#

private void  theControl_OLEDragOverEvent( DragEventArgs e,  short State)
{
   int X = e.X;
   int Y = e.Y;
   int Button = e.KeyState | 0x100000;
   int Shift = (int)Control.ModifierKeys | 0x10000;
   Support.ConvertCoordinatesToClient(Text1, ref X, ref Y);

  //User's code
   if (e.Data.GetDataPresent(DataFormats.Text))
   {
      e.Effect = DragDropEffects.Copy;
   }
  else
   {
      e.Effect = DragDropEffects.None;
   }
   //End of user's code
}

VB.NET

Private Sub theControl_OLEDragOverEvent(ByRef e As DragEventArgs, ByRef State As Short)
   Dim x As Integer = e.X
   Dim y As Integer = e.Y
   Dim Button As Integer = e.KeyState \ &H100000
   Dim Shift As Integer = Control.ModifierKeys \ &H10000
   Support.ConvertCoordinatesToClient(theControl, x, y)
   'User's code
   If e.Data.GetDataPresent(DataFormats.Text) Then
      e.Effect = DragDropEffects.Copy
   Else
      e.Effect = DragDropEffects.None
   End If
   'End of user's code
End Sub

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#

private void theControl_DragEnter(object sender, DragEventArgs e)
{
   e.Effect = DragDropEffects.All;
   theControl_OLEDragOverEvent(e, 0);
}

private void theControl_DragLeave(object sender, EventArgs e)
{
   theControl_OLEDragOverEvent(new DragEventArgs(null,0,0,0,DragDropEffects.All,DragDropEffects.All), 1);
}

private void theControl_DragOver(object sender, DragEventArgs e)
{
   theControl_OLEDragOverEvent(e, 2);
}

VB.NET

Private Sub theControl_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles theControl.DragEnter
   e.Effect = DragDropEffects.All
   theControl_OLEDragOverEvent(e, 0)
End Sub

Private Sub theControl_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles theControl.DragLeave
   theControl_OLEDragOverEvent(New DragEventArgs(Nothing, 0, 0, 0, DragDropEffects.All, DragDropEffects.All), 1)
End Sub

Private Sub theControl_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles theControl.DragOver
   theControl_OLEDragOverEvent(e, 2)
End Sub

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#

class Support
{

   /// <summary>
   /// Convert .NET event's screen coordinates to client control 
   /// coordinates to be used by converted drag and drop members
   /// </summary>
   /// <param name="clientControl">Control to where the coordinates will be traslated</param>
   /// <param name="x">X Coordinate</param>
   /// <param name="y">Y Coordinate</param>
   public void ConvertCoordinatesToClient(Control clientControl, 
ref int x, ref int y)
   { 
      Point  convertedCoordinates = clientControl.PointToClient
(new Point(x,y));
      x = convertedCoordinates.X;
      y = convertedCoordinates.Y;
   }
}

VB.NET

'Convert .NET event's screen coordinates to client control coordinates to be used by converted drag and drop members
Public Sub ConvertCoordinatesToClient(ByVal clientControl As Control, ByRef x As Integer, ByRef y As Integer)
   Dim convertedCoordinates As Point = clientControl.PointToClient(New Point(x, y))
   x = convertedCoordinates.X
   y = convertedCoordinates.Y
End Sub

Additional code for controls with no DragOver handlers in source

Add a .NET DragEnter event handler and add the following line of code

C#

private void theControl_DragEnter(object sender, DragEventArgs e)
{
   //UPGRADE_WARNING: The following statement was inserted to allow this control to receive drag and drop actions
   e.Effect = DragDropEffects.All;
}

C# requires to register the event in the designer code

this.theControl.DragEnter += new DragEventHandler(this.theControl_DragEnter);

VB.NET

Private Sub theControl_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles theControl.DragEnter

   'UPGRADE_WARNING: The following statemente was inserted to allow this control to receive drag and drop actions 
   e.Effect = DragDropEffects.All

End Sub

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

Private Sub theControl_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
   'User's code
   If Data.GetFormat(vbCFText) Then
      theControl.Text = Data.GetData(vbCFText)
   End If
   'End of user's code
End Sub

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#

private void Text1_DragDrop(object sender, DragEventArgs e)
{   
   int X = e.X;
   int Y = e.Y;
   int Button = e.KeyState | 0x100000;
   int Shift = (int)Control.ModifierKeys | 0x10000;
   Support.ConvertCoordinatesToClient(Text1, ref X, ref Y);
   //User's code                    
   if (e.Data.GetDataPresent(DataFormats.Text))
   {
      Text1.Text = Convert.ToString(e.Data.GetData(DataFormats.Text));
   } 
   //End of user's code
}

VB.NET

Private Sub theControl _DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles theControl.DragDrop
   Dim X As Single = e.X
   Dim Y As Single = e.Y
   Dim Button As Integer = e.KeyState \ &H100000
   Dim Shift As Integer = Control.ModifierKeys \ &H10000
   ConvertCoordinatesToClient(theControl, X, Y)
   'User's code
   If e.Data.GetDataPresent(DataFormats.Text) Then
      theControl.Text = CStr(e.Data.GetData(DataFormats.Text))
   End If
   'End of user's code

End Sub

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

Private Sub theControl_OLECompleteDrag(Effect As Long)
  'Code to process the event
End Sub

Expected code

C#

private void theControl_OLECompleteDrag(int Effect)
{
  //Code to process the event
}

VB.NET

Private Sub theControl_OLECompleteDrag(ByVal Effect As Long)
  'Code to process the event
End Sub

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#

private void  theControl_OLEStartDrag( )
{
   DragDropEffects effect;
   effect = theControl.DoDragDrop(theControl.SelectedText, DragDropEffects.Copy);

   //UPGRADE_WARNING: The following statement was inserted to support conversion of Drag and Drop
   theControl_OLECompleteDrag((int)effect);
}

VB.NET

Private Sub theControl_OLEStartDrag()
   Dim Effect As DragDropEffects
   Effect = theControl.DoDragDrop(theControl.SelectedText, DragDropEffects.Copy)
   
   'UPGRADE_WARNING: The following statement was inserted to support conversion of Drag and Drop
   theControl_OLECompleteDrag(Effect)

End Sub

OLEGiveFeedback

This event is raised whenever the mouse passes over a possible target, during a drag and drop operation.

Source code

Private Sub theControl_OLEGiveFeedback(Effect As Long, DefaultCursors As Boolean)
    
   'User's code
    If Effect = vbDropEffectNone Then
        theControl.ForeColor = RGB(255, 0, 0)
    Else
        theControl.ForeColor = RGB(0, 0, 0)
    End If
    
    Dim defCursors As Boolean
    defCursors = DefaultCursors
   'End of user's code

End Sub

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#

private void theControl_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
   //User's code
   if (e.Effect == ((int)DragDropEffects.None))
   {
      theControl.ForeColor = Color.FromArgb(255, 0, 0);
   }
   else
   {
      theControl.ForeColor = Color.FromArgb(0, 0, 0);
   }

   bool defCursors;
   defCursors = e.UseDefaultCursors;
   //End of user's code
}

VB.NET

Private Sub theControl_GiveFeedback(ByVal sender As Object, ByVal e As GiveFeedbackEventArgs) Handles Text1.GiveFeedback

   'User's code 
   If e.Effect = DragDropEffects.None Then
      theControl.ForeColor = Color.FromArgb(255, 0, 0)
   Else
      theControl.ForeColor = Color.FromArgb(0, 0, 0)
   End If

   Dim defCursors As Boolean
   defCursors = e.UseDefaultCursors
   'End of user's code

End Sub

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.

Private Sub theControl_OLESetData(Data As DataObject, DataFormat As Integer)
   If DataFormat = vbCFText Then
      Data.SetData "Text", vbCFText
   Else
      Data.SetData "RTF", vbCFRTF
   End If 
End Sub

Note the empty value parameter in the Data.SetData method call.

Private Sub theControl_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
        Data.SetData , vbCFText
        AllowedEffects = vbDropEffectCopy
End Sub

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#

private void Text2_OLEStartDrag()
{
   DragDropEffects effect;
   //Data.SetData( , vbCFText);
   //AllowedEffects = (int) DragDropEffects.Copy;
                
   //UPGRADE_WARNING: Drag and Drop delayed data setting behavior was removed.    
   effect = Text2.DoDragDrop("Text", DragDropEffects.Copy);
   Text2_OLECompleteDrag((int)effect);
}

//UPGRADE_WARNING: Drag and Drop delayed data setting behavior was removed.            
//private void  Text2_OLESetData( DataObject Data,  short DataFormat)
//{
//    if ( DataFormat = vbCFText ) 
//    {
//        Data.SetData("Text", vbCFText);
//    }
//    else
//    {
//        Data.SetData("RTF", vbCFRTF);
//    }
//}

VB.NET

Private Sub Text1_OLEStartDrag()
   Dim Effect As DragDropEffects
   'Data.SetData(, vbCFText)
   'UPGRADE_WARNING: Drag and Drop delayed data setting behavior was removed.
   Effect = Text1.DoDragDrop("Text", DragDropEffects.Copy)
End Sub


'UPGRADE_WARNING: Drag and Drop delayed data setting behavior was removed.
'Private Sub Text1_OLESetData(ByRef Data As DataObject, ByRef DataFormat As Short)
'    If DataFormat = vbCFText Then
'           Data.SetData("Text", vbCFText)
'       Else
'           Data.SetData("RTF", vbCFRTF)
'       End If
'End Sub

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.

theControl.OLEDragMode = vbAutomatic

VB6 Manual drag mode requires coding all the drag and drop handling. This is the same as .NET drag and drop.

theControl.OLEDragMode = vbManual

Expected code

Automatic drag and drop is not supported in .NET

C#

//UPGRADE_WARNING: Control.DragMode was not upgraded.
//theControl.OLEDragMode = vbAutomatic

VB.NET

'UPGRADE_WARNING: Control.DragMode was not upgraded.
'theControl.OLEDragMode = vbAutomatic

Manual drag and mode is the default behavior of .NET drag and drop. All drag and drop handling must be coded.

C#

//UPGRADE_WARNING: Control.DragMode was not upgraded.
//theControl.OLEDragMode = vbManual

VB.NET

'UPGRADE_WARNING: Control.DragMode was not upgraded.
'theControl.OLEDragMode = vbManual

Last updated