Export (0) Print
Expand All

Drag and Drop Changes in Visual Basic .NET

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

In Visual Basic .NET, a single model for drag and drop is used. It is similar to OLE drag and drop, but the names and parameters for drag-and-drop methods and events are different, and the event model is different. Drag-and-drop code cannot automatically be upgraded to Visual Basic .NET; it must be rewritten using the new model.

The following example shows the differences when 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 Basic .NET
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   ' Dropping must be enabled before the dragging occurs.
   TextBox2.AllowDrop = True
End Sub

Private Sub TextBox1_MouseDown(ByVal sender As Object, 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)
End Sub

Private Sub TextBox2_DragEnter(ByVal sender As Object, 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
   End If
End Sub

Private Sub TextBox2_DragDrop(ByVal sender As Object, 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
End Sub

See Also

Drag-and-Drop Operations and Clipboard Support

Show:
© 2014 Microsoft