Drag and Drop for Visual Basic 6.0 Users

The model for implementing drag-and-drop editing in Visual Basic 2008 differs considerably from the Visual Basic 6.0 model.

Conceptual Differences

In Visual Basic 6.0, drag-and-drop editing can be accomplished by two different methods: standard dragging, for dragging between controls on a form, and OLE dragging, for dragging between forms and applications.

In Visual Basic 2008, a single model for drag-and-drop editing is used. It is similar to OLE dragging, but the names and parameters for drag-and-drop methods and events are different, and the event model is different.

Code Changes for Drag and Drop

Code Changes for Dragging and Dropping Text

The following example shows the differences in code for dragging text from one TextBox control to another.

' Visual Basic 6.0
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Text1.Text = "Hello World"
    ' Begin dragging by calling the OLEDrag method.
    Text1.OLEDrag
End Sub

Private Sub Text1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
    ' Only allow copying.
    AllowedEffects = vbDropEffectCopy
    Data.Clear
    ' Populate the Data object with the text to copy and the format.
    Data.SetData Text1.Text, vbCFText
End Sub

Private Sub Text2_OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer)
    ' Make sure that the format is text.
    If Data.GetFormat(vbCFText) Then
      ' If it is text, enable dropping for the second TextBox.
      Text2.OLEDropMode = vbOLEDropManual
    End If
End Sub

Private Sub Text2_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
    ' Copy the text from the Data object to the second TextBox.
    Text2.Text = Data.GetData(vbCFText)
End Sub
' Visual BasicPrivateSub Form1_Load(ByVal sender As System.Object, ByVal e _
As System.EventArgs) HandlesMyBase.Load

    ' Dropping must be enabled before the dragging occurs.
    TextBox2.AllowDrop = TrueEndSubPrivateSub TextBox1_MouseDown(ByVal sender AsObject, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown

    TextBox1.Text = "Hello World"    ' Begin dragging by calling the DoDragDrop method.    ' OLEStartDrag is replaced by arguments on the method.
    TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
EndSubPrivateSub TextBox2_DragEnter(ByVal sender AsObject, ByVal e _
As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter

    ' Make sure that the format is text.If (e.Data.GetDataPresent(DataFormats.Text)) Then      ' Allow drop.
      e.Effect = DragDropEffects.Copy
    Else      ' Do not allow drop.
      e.Effect = DragDropEffects.None
    EndIfEndSubPrivateSub TextBox2_DragDrop(ByVal sender AsObject, ByVal e _
As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop

    ' Copy the text to the second TextBox.
    TextBox2.Text = e.Data.GetData(DataFormats.Text).ToString
EndSub

Upgrade Notes

Drag-and-drop code cannot automatically be upgraded to Visual Basic 2008; it must be rewritten using the new model. Any drag-and-drop code is marked with upgrade warnings during the upgrade process.

See Also

Other Resources

Drag-and-Drop Operations and Clipboard Support