467,910 Members | 1,578 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,910 developers. It's quick & easy.

Drag & Drop...

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
Nov 21 '05 #1
  • viewed: 1954
Share:
1 Reply

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

"Terry Olsen" <to******@hotmail.com> wrote in message
news:OY**************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Grey | last post: by
2 posts views Thread by Ivo Tcholakov | last post: by
6 posts views Thread by jojobar | last post: by
3 posts views Thread by Goldwind | last post: by
3 posts views Thread by VB Programmer | last post: by
5 posts views Thread by sesar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.