请参考以下文章:HOW TO: Add TreeView Drag-and-Drop Functionality in a Visual Basic .NET Application (Q307967)
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q307967
学习如何实现拖动的功能,这篇文章中提供了如何在两个Treeview中拖动结点的例子,您也可以根据自己的需要改写为在一个treeview中拖动结点的例子。
' Handles the DragDrop event for both TreeView controls.
Private Sub TreeView_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles tvwLeft.DragDrop, tvwRight.DragDrop
' Initialize variable that holds the node dragged by the user.
Dim OriginationNode As TreeNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)
' Calling GetDataPresent is a little different for a TreeView than for an
' image or text because a TreeNode is not a member of the DataFormats
' class. That is, it's not a predefined type. As such, you need to use a
' different overload, one that takes the type as a string.
If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", False) Then
Dim pt As Point
Dim DestinationNode As TreeNode
' Use PointToClient to compute the location of the mouse over the
' destination TreeView.
pt = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
' Use this Point to get the closest node in the destination TreeView.
DestinationNode = CType(sender, TreeView).GetNodeAt(pt)
' The If statement ensures that the user doesn't completely lose the
' node if they accidentally release the mouse button over the node they
' attempted to drag. Without a check to see if the original node is the
' same as the destination node, they could make the node disappear.
If Not DestinationNode.TreeView Is OriginationNode.TreeView Then
DestinationNode.Nodes.Add(CType(OriginationNode.Clone, TreeNode))
' Expand the parent node when adding the new node so that the drop
' is obvious. Without this, only a + symbol would appear.
DestinationNode.Expand()
' If the Ctrl key was not pressed, remove the original node to
' effect a drag-and-drop move.
If (e.KeyState And CtrlMask) <> CtrlMask Then
OriginationNode.Remove()
End If
End If
End If
End Sub
' Handles the DragEnter event for both TreeView controls.
Private Sub TreeView_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles tvwRight.DragEnter, tvwLeft.DragEnter
' Check to be sure that the drag content is the correct type for this
' control. If not, reject the drop.
If (e.Data.GetDataPresent("System.Windows.Forms.TreeNode")) Then
' If the Ctrl key was pressed during the drag operation then perform
' a Copy. If not, perform a Move.
If (e.KeyState And CtrlMask) = CtrlMask Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.Move
End If
Else
e.Effect = DragDropEffects.None
End If
End Sub
' Handles the ItemDrag event for both TreeView controls.
Private Sub TreeView_ItemDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles tvwRight.ItemDrag, tvwLeft.ItemDrag
If e.Button = MouseButtons.Left Then
'invoke the drag and drop operation
DoDragDrop(e.Item, DragDropEffects.Move Or DragDropEffects.Copy)
End If
End Sub