Drag & Drop...

T

Terry Olsen

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.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.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.TreeNode"), 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
 
R

Roger Rabbit

1. Im not sure what your asking here. If you just want a path then why cant
you store it in a string? If you want the tree list structure then just
clone the node thats interests you (or its root) and store a reference to
this node in a variable.

2. If Not (selectedTreeview.SelectedNode Is targetNode)

If target node is Nothing then the code in the conditional still executes.
You then try to access a property of Nothing which is why the excepion gets
thrown.

targetNode.ImageIndex

I would chnage the conditional too

If Not targetNot Is Nothing AndAlso (selectedTreeview.SelectedNode Is
targetNode) then

RR
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top