1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| Private Sub treeView1_ItemDrag(ByVal sender As Object, ByVal e As ItemDragEventArgs) Handles TreeView1.ItemDrag
' Move the dragged node when the left mouse button is used.
If e.Button = MouseButtons.Left Then
DoDragDrop(e.Item, DragDropEffects.Move)
' Copy the dragged node when the right mouse button is used.
ElseIf e.Button = MouseButtons.Right Then
DoDragDrop(e.Item, DragDropEffects.Copy)
End If
End Sub 'treeView1_ItemDrag
' Set the target drop effect to the effect
' specified in the ItemDrag event handler.
Private Sub treeView1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeView1.DragEnter
e.Effect = e.AllowedEffect
End Sub 'treeView1_DragEnter
' Select the node under the mouse pointer to indicate the
' expected drop location.
Private Sub treeView1_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeView1.DragOver
' Retrieve the client coordinates of the mouse position.
Dim targetPoint As Point = TreeView1.PointToClient(New Point(e.X, e.Y))
' Select the node at the mouse position.
TreeView1.SelectedNode = TreeView1.GetNodeAt(targetPoint)
End Sub 'treeView1_DragOver
Private Sub treeView1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeView1.DragDrop
' Retrieve the client coordinates of the drop location.
Dim targetPoint As Point = TreeView1.PointToClient(New Point(e.X, e.Y))
' Retrieve the node at the drop location.
Dim targetNode As TreeNode = TreeView1.GetNodeAt(targetPoint)
' Retrieve the node that was dragged.
Dim draggedNode As TreeNode = CType(e.Data.GetData(GetType(TreeNode)), TreeNode)
' Confirm that the node at the drop location is not
' the dragged node or a descendant of the dragged node.
If Not draggedNode.Equals(targetNode) AndAlso Not ContainsNode(draggedNode, targetNode) Then
If Not targetNode Is Nothing Then
' If it is a move operation, remove the node from its current
' location and add it to the node at the drop location.
If e.Effect = DragDropEffects.Move Then
draggedNode.Remove()
targetNode.Nodes.Add(draggedNode)
' If it is a copy operation, clone the dragged node
' and add it to the node at the drop location.
ElseIf e.Effect = DragDropEffects.Copy Then
targetNode.Nodes.Add(CType(draggedNode.Clone(), TreeNode))
End If
' Expand the node at the location
' to show the dropped node.
targetNode.Expand()
End If
End If
End Sub 'treeView1_DragDrop
' Determine whether one node is a parent
' or ancestor of a second node.
Private Function ContainsNode(ByVal node1 As TreeNode, ByVal node2 As TreeNode) As Boolean
If Not node2 Is Nothing Then
' Check the parent node of the second node.
If node2.Parent Is Nothing Then
Return False
End If
If node2.Parent.Equals(node1) Then
Return True
End If
' If the parent node is not null or equal to the first node,
' call the ContainsNode method recursively using the parent of
' the second node.
Return ContainsNode(node1, node2.Parent)
Else
Return 0
End If
End Function 'ContainsNode
Private Sub ButtonSupprimerElements_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSupprimerElements.Click
TreeView1.Nodes.Remove(TreeView1.SelectedNode)
End Sub |
Partager