Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
TitleDrag and drop using VB .NET
Keywordsdrag and drop, drag, drop, VB.NET, AllowDrop
CategoriesVB.NET, Software Engineering, Controls
 
Set the AllowDrop property to True for any control that you want to accept drops.

Start a drag by calling a control's DoDragDrop method. Pass the method the data you want to drag and the drag effects you want to allow (copy, link, and so forth). This program demonstrates two kinds of drag. The first drags a Label control. The second drags a text value.

 
Private Sub lblLabelSource1_MouseDown(ByVal sender As _
    System.Object, ByVal e As _
    System.Windows.Forms.MouseEventArgs) Handles _
    lblLabelSource1.MouseDown
    ' Start a drag.
    lblLabelSource1.DoDragDrop( _
        lblLabelSource1, _
        DragDropEffects.Copy)
End Sub

Private Sub lblTextSource1_MouseDown(ByVal sender As _
    Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles lblTextSource1.MouseDown
    ' Start a drag.
    lblTextSource1.DoDragDrop( _
        lblTextSource1.Text, _
        DragDropEffects.Copy)
End Sub
 
The rest of the action takes place in the drag target control's event handlers. In the DragEnter event, check the data to see if it is the kind the control wants. Set the event handler's e.Effect value to indicate the effect that the mouse should display.
 
' Display the copy effect.
Private Sub lblLabelTarget_DragEnter(ByVal sender As _
    System.Object, ByVal e As _
    System.Windows.Forms.DragEventArgs) Handles _
    lblLabelTarget.DragEnter
    ' See if the data includes a Label.
    If e.Data.GetDataPresent(GetType(Label)) Then
        ' There is Label data. Allow copy.
        e.Effect = DragDropEffects.Copy

        ' Highlight the control.
        lblLabelTarget.BorderStyle = BorderStyle.FixedSingle
    Else
        ' There is no Label. Prohibit drop.
        e.Effect = DragDropEffects.None
    End If
End Sub

' Display the copy effect.
Private Sub lblTextTarget_DragEnter(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DragEventArgs) Handles _
    lblTextTarget.DragEnter
    ' See if the data includes text.
    If e.Data.GetDataPresent(DataFormats.Text) Then
        ' There is text. Allow copy.
        e.Effect = DragDropEffects.Copy
    Else
        ' There is no text. Prohibit drop.
        e.Effect = DragDropEffects.None
    End If
End Sub
 
In the DragLeave event, you can remove any highlighting or other visual effects you added to the control during DragEnter.
 
' Unhighlight the control.
Private Sub lblLabelTarget_DragLeave(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    lblLabelTarget.DragLeave
    lblLabelTarget.BorderStyle = BorderStyle.Fixed3D
End Sub
 
In the DragDrop event, get the dropped data and do whatever is appropriate with it.
 
' Perform the drop.
Private Sub lblLabelTarget_DragDrop(ByVal sender As _
    System.Object, ByVal e As _
    System.Windows.Forms.DragEventArgs) Handles _
    lblLabelTarget.DragDrop
    Dim lbl As Label = DirectCast( _
        e.Data.GetData(GetType(Label)), Label)
    lblLabelTarget.Text = lbl.Text
    lblLabelTarget.BackColor = lbl.BackColor
    lblLabelTarget.BorderStyle = BorderStyle.Fixed3D
End Sub

Private Sub lblTextTarget_DragDrop(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DragEventArgs) Handles _
    lblTextTarget.DragDrop
    lblTextTarget.Text = _
        e.Data.GetData(DataFormats.Text).ToString
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated