Title | Drag and drop objects in VB .NET |
Description | This example shows how to drag and drop objects in Visual Basic .NET. The trick is setting the Serializable attribute on the class you will drag so the drag source can serialize it and the drop target can deserialize it. |
Keywords | drag and drop, object, VB.NET |
Categories | VB.NET, Controls |
|
|
The trick is setting the Serializable attribute on the class you will drag so the drag source can serialize it and the drop target can deserialize it. The serialization and deserialization happen automatically; you just need to set this attribute.
The Person class shown here contains a couple of constructors to let you initialize an object using separate string values or a string that combines name, street, city, state, and ZIP data.
|
|
Imports System.Runtime.Serialization
<Serializable()> _
Public Class Person
Public Name As String
Public Street As String
Public City As String
Public State As String
Public Zip As String
' Make a new Person from Name, Street,
' City, State, and Zip.
Public Sub New(ByVal new_name As String, ByVal _
new_street As String, ByVal new_city As String, _
ByVal new_state As String, ByVal new_zip As String)
Name = new_name
Street = new_street
City = new_city
State = new_state
Zip = new_zip
End Sub
' Make a new Person from Name, Street,
' City, State, and Zip all in one string.
Public Sub New(ByVal txt As String)
txt = txt.Replace(vbCrLf, ";")
Dim lines() As String = txt.Split(";"c)
Name = lines(0)
Street = lines(1)
Dim city_state_zip() As String = lines(2).Split(" " & _
""c)
City = city_state_zip(0)
State = city_state_zip(1)
Zip = city_state_zip(2)
End Sub
End Class
|
|
When it loads, this program assigns the same MouseDown event handler to its three label controls stored in the Labels array. It also assigns DragEnter and DragDrop event handlers to the lstPersonInfo control.
|
|
' Give the Labels the same event handlers.
For i As Integer = labels.GetLowerBound(0) To _
labels.GetUpperBound(0)
AddHandler labels(i).MouseDown, AddressOf _
label_MouseDown
Next i
AddHandler lstPersonInfo.DragEnter, AddressOf list_DragEnter
AddHandler lstPersonInfo.DragDrop, AddressOf list_DragDrop
|
|
When the user presses the mouse down on one of the drag source labels, the label_MouseDown event handler verifies that the user is pressing the right mouse button. It creates a Person object initialized from the label's contents and calls the control's DoDragDrop method to start the drag, passing it the Person object.
|
|
' If this is the right mouse button,
' start a drag.
Private Sub label_MouseDown(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs)
' Make sure this is the right mouse button.
If Button.MouseButtons <> MouseButtons.Right Then Exit _
Sub
' Make a Person object containing the data.
Dim lbl As Label = DirectCast(sender, Label)
Dim source_person As New Person(lbl.Text)
' Start the drag.
lbl.DoDragDrop(source_person, DragDropEffects.Copy)
End Sub
|
|
When the user drags over the ListBox, the list_DragEnter event handler uses the GetDataPresent method to see if the drag contains data of the type howto_net_drag_drop_object.Person. The value howto_net_drag_drop_object is the program's root namespace and the Person class is defined within that namespace. If the data contains this type of data, the event handler allows a Copy operation.
|
|
' See if we want to allow a drop here.
Private Sub list_DragEnter(ByVal sender As Object, ByVal e _
As System.Windows.Forms.DragEventArgs)
' Make sure there is Person data.
If e.Data.GetDataPresent("howto_net_drag_drop_object.Person") _
Then
e.Effect = DragDropEffects.Copy
Else
' There is no Person data.
e.Effect = DragDropEffects.None
End If
End Sub
|
|
Finally, when the user drops the data over the ListBox, the list_DragDrop event handler executes. It uses GetData to get the howto_net_drag_drop_object.Person data and casts it into a Person object. It then displays the object's fields.
|
|
' Finish the drag.
Private Sub list_DragDrop(ByVal sender As Object, ByVal e _
As System.Windows.Forms.DragEventArgs)
' Get the Person data.,
Dim the_person As Person = DirectCast( _
e.Data.GetData("howto_net_drag_drop_object.Person"), _
Person)
' Display the person's data.
lstPersonInfo.Items.Clear()
lstPersonInfo.Items.Add(the_person.Name)
lstPersonInfo.Items.Add(the_person.Street)
lstPersonInfo.Items.Add(the_person.City & " " & _
the_person.State & " " & the_person.Zip)
End Sub
|
|
|
|