My first time using TreeViews. I have TreeView1 set up to display my
directory structure just like Windows Explorer. I can drag & drop files and
folders over to TreeView2. I can re-arrange the structure in TreeView2
(putting files in different folders and such...like you do when you create a
structure for burning a CD).
I have two questions.
1. How can I remember the full path from TreeView1 (when it comes time to
actually do something with the arrangement in TreeView2)?
2. I have not been able to figure out how to get the DragOver part to work
correctly. In the code below, when I drag over a file node, it's supposed
to display the "not here" cursor. When I drag over a directory node (or the
root), it's supposed to display the "drop" cursor. It does fine except when
I drag over a file node and then drag off the file node. When I drag over
the file node, the cursor changes to "not here". When I drag off the file
node, I get an exception "object reference not set to an instance, try using
the NEW keyword". Looking for help to correct this.
Thanks.
Public Sub TreeViewISO_DragOver(ByVal sender As System.Object, ByVal e As
DragEventArgs) Handles TreeViewISO.DragOver
'Check that there is a TreeNode being dragged
If e.Data.GetDataPresent("System.Windows.Forms.TreeNo de", 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.SelectedNode 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.TreeNod e"), TreeNode)
If targetNode.ImageIndex = 2 Then <== Exception thrown here...
e.Effect = DragDropEffects.None
Else
'Currently selected node is a suitable target
e.Effect = DragDropEffects.Move
End If
End If
End Sub