Is it posible to replace the dragged item and the target item easily in the same treeview? Here is my code:
Private Sub TV_ServerList_ItemDrag(sender As Object, e As ItemDragEventArgs) Handles TV_ServerList.ItemDrag, TV_Playlistcomposer.ItemDrag
'Set the drag node and initiate the DragDrop
DoDragDrop(e.Item, DragDropEffects.Copy)
End Sub
Private Sub TV_ServerList_DragEnter(sender As Object, e As DragEventArgs) Handles TV_ServerList.DragEnter, TV_Playlistcomposer.DragEnter
'See if there is a TreeNode being dragged
If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) Then
'TreeNode found allow move effect
e.Effect = DragDropEffects.Copy
Else
'No TreeNode found, prevent move
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub TV_ServerList_DragOver(sender As Object, e As DragEventArgs) Handles TV_ServerList.DragOver, TV_Playlistcomposer.DragOver
'Check that there is a TreeNode being dragged
If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) = False Then Exit Sub
'Get the TreeView raising the event (incase multiple on form)
Dim selectedTreeview As TreeView = CType(sender, TreeView)
'As the mouse moves over nodes, provide feedback to the user
'by highlighting the node that is the current drop target
Dim pt As Point = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
Dim targetNode As TreeNode = selectedTreeview.GetNodeAt(pt)
'See if the targetNode is currently selected, if so no need to validate again
If Not (selectedTreeview Is targetNode) Then
'Select the node currently under the cursor
selectedTreeview.SelectedNode = targetNode
'Check that the selected node is not the dropNode and also that it
'is not a child of the dropNode and therefore an invalid target
Dim dropNode As TreeNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)
Do Until targetNode Is Nothing
If targetNode Is dropNode Then
e.Effect = DragDropEffects.None
Exit Sub
End If
targetNode = targetNode.Parent
Loop
End If
'Currently selected node is a suitable target, allow the move
e.Effect = DragDropEffects.Copy
End Sub
Private Sub TV_ServerList_DragDrop(sender As Object, e As DragEventArgs) Handles TV_Playlistcomposer.DragDrop
'Check that there is a TreeNode being dragged
If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) = False Then Exit Sub
'Get the TreeView raising the event (incase multiple on form)
Dim selectedTreeview As TreeView = CType(sender, TreeView)
'Get the TreeNode being dragged
Dim dropNode As TreeNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)
Dim targetNode As TreeNode = selectedTreeview.SelectedNode
If dropNode.Nodes.Count = 0 And targetNode.Nodes.Count = 0 And targetNode.Text(0) <> "#" Then
Try
Dim cat As TreeNode = dropNode
dropNode = targetNode.Clone()
targetNode = cat.Clone()
'Ensure the newley created node is visible to the user and select it
dropNode.EnsureVisible()
selectedTreeview.SelectedNode = targetNode
Catch ex As Exception
MsgBox(ex.ToString)
End Try
ElseIf dropNode.Nodes.Count = 0 Then
'The target node should be selected from the DragOver event
If targetNode.Text(0) = "#" Then
'Remove the drop node from its current location
'dropNode.Remove()
Dim addnode As TreeNode = dropNode.Clone()
'If there is no targetNode add dropNode to the bottom of the TreeView root
'nodes, otherwise add it to the end of the dropNode child nodes
If targetNode Is Nothing Then
selectedTreeview.Nodes.Add(addnode)
Else
targetNode.Nodes.Add(addnode)
End If
'Ensure the newley created node is visible to the user and select it
dropNode.EnsureVisible()
selectedTreeview.SelectedNode = addnode
End If
End If
End Sub
The goal here is to move items with no children from one treeview to another only to blocks starting with "#". When the items have been moved I want to be able to change their order, and move around other "#" blocks via drag and drop. I also need to be able to change the order of the "#" blocks