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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class FormTwoTreeViewDragDrop
Private treeView1 As TreeView
Private treeView2 As TreeView
Public Sub New()
' Cet appel est requis par le concepteur.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
treeView1 = New TreeView()
treeView2 = New TreeView()
Me.SuspendLayout()
' Initialize treeView1.
treeView2.AllowDrop = True
treeView1.Dock = DockStyle.Left
treeView2.Dock = DockStyle.Right
' Add nodes to treeView1.
Dim node As TreeNode
Dim x As Integer
For x = 0 To 3
' Add a root node to treeView1.
node = treeView1.Nodes.Add(String.Format("Node{0}", x * 4))
Dim y As Integer
For y = 1 To 4
' Add a child node to the previously added node.
node = node.Nodes.Add(String.Format("Node{0}", x * 4 + y))
Next y
Next x
' Add nodes to treeView2.
For x = 0 To 3
' Add a root node to treeView2.
node = treeView2.Nodes.Add(String.Format("Node{0}", x * 5))
Dim y As Integer
For y = 1 To 4
' Add a child node to the previously added node.
node = node.Nodes.Add(String.Format("Node{0}", x * 5 + y))
Next y
Next x
' Add event handlers treeView1 for the required drag events.
AddHandler treeView1.ItemDrag, AddressOf treeView1_ItemDrag
' Add event handlers treeView2 for the required drag events.
AddHandler treeView2.DragEnter, AddressOf treeView2_DragEnter
AddHandler treeView2.DragOver, AddressOf treeView2_DragOver
AddHandler treeView2.DragDrop, AddressOf treeView2_DragDrop
' Initialize the form.
Me.ClientSize = New Size(292, 273)
Me.Controls.Add(treeView1)
Me.Controls.Add(treeView2)
Me.ResumeLayout(False)
End Sub 'New
Private Sub treeView1_ItemDrag(ByVal sender As Object, ByVal e As ItemDragEventArgs)
' 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 treeView2_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
e.Effect = e.AllowedEffect
End Sub 'treeView2_DragEnter
' Select the node under the mouse pointer to indicate the
' expected drop location.
Private Sub treeView2_DragOver(ByVal sender As Object, ByVal e As DragEventArgs)
' Retrieve the client coordinates of the mouse position.
Dim targetPoint As Point = treeView2.PointToClient(New Point(e.X, e.Y))
' Select the node at the mouse position.
treeView2.SelectedNode = treeView2.GetNodeAt(targetPoint)
End Sub 'treeView2_DragOver
Private Sub treeView2_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
' Retrieve the client coordinates of the drop location.
Dim targetPoint As Point = treeView2.PointToClient(New Point(e.X, e.Y))
' Retrieve the node at the drop location.
Dim targetNode As TreeNode = treeView2.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 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 Sub 'treeView2_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
' 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)
End Function 'ContainsNode
End Class 'Form1 |
Partager