We Are GAP Mobilize
Free Assessment Tool

VB to .NET

2. Conversion of Standard Drag and Drop

2.1 Drag method

Calling the Drag method starts the drag and drop operation.

2.1.1. 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

2.1.2. 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);

2.1.3. 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.

Visual Basic .NET

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

C#

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

2.2 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.

2.2.1 Source code

Private Sub theControl_DragOver(Source As Control, x As Single, y As Single, State As Integer)

    '<code to handle event>
     
End Sub

2.2.2. 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.

Visual Basic .NET

theControl.AllowDrop = True

C#

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.

Visual Basic .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

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   
}

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.

Visual Basic .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

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

C#

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

2.2.3. 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

Visual Basic .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

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;
   }
}

2.2.4. 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:

Visual Basic .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

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

C#

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

2.3 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.

2.3.1. Source code

Private Sub theControl_DragDrop(Source As Control, x As Single, y As Single)

    'User's code to handle event

End Sub

2.3.2. 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.

Visual Basic .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

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

   }
}

2.4 DragIcon property

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

2.4.1. Source code

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

2.4.2. Expected code

Visual Basic .NET

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

C#

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

2.5 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.

2.5.1. 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 to code all the drag and drop handling. This is the same as .NET drag and drop.

theControl.DragMode = vbManual

2.5.2 Expected code

Automatic drag and drop is not supported in .NET

Visual Basic .NET

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

C#

//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.

Visual Basic .NET

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

C#

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


See also:

Talk To An Engineer