Treeview adds nodes to itself without apparent cause
-
Hi, I am using a treeview and I saw a neat way of adding your own type of nodes using a class derived from TreeNode. My class is the same as a tree node but it has a key property I can use to index into collections of other stuff class TreeNodeCommand Inherits TreeNode Public Key as string End Class In order to make my code slick I then wrote two conversion functions, so that any nodes returned from treeview are converted to TreeNodeCommand... Private Function CurrentNode() As TreeNodeCommand Return ToTreeNodeCommand(tvwCommands.SelectedNode) End Function Private Function RootNode() As TreeNodeCommand Return ToTreeNodeCommand(tvwCommands.Nodes.Item(0)) End Function ' Convert tree node item to tree node command item Private Function ToTreeNodeCommand(ByRef prItem As TreeNode) As TreeNodeCommand Return CType(prItem, TreeNodeCommand) End Function OK, thats the background. I have a treeview that refers to TreeNodeCommands rather than TreeNodes. I have drag and drop support on this treeview, but I dont want the user to be able to drop a particular thing on the Root node. So in my dragover code I select the node in the treeview under the mouse and using my CurrentNode and RootNode properties I check to see if they are the same node.... If Not CurrentNode Is RootNode Then e.Effect = DragDropEffects.Copy End If ...but guess what. Every single time the IF statement executes, I get another node added to the tree, which is effectively a CLONE of the root node. If I do this, which is perfectly acceptable, but not as slick to read then all is well... If Not tvwCommands.SelectedNode Is tvwCommands.Nodes(0) Then e.Effect = DragDropEffects.Copy End If You will have had to see this to believe it. No where does any single line of code get executed that does a ...Nodes.Add. Yet, new nodes do appear. I can work around this, but I would be really interested to see if for some reason you think the Clone method of the root node is being invoked by the type conversion, or by some other means. I think that's what it is. The interesting thing is I override the Clone method in my derived class and it doesn't get called at any time. So, I am lost in la la land with this one people. Any thoughts or similar experiences would be appreciated. Perhaps I am missing something due to my lack of .NET experience. Apologies for the lengthy post.
-
Hi, I am using a treeview and I saw a neat way of adding your own type of nodes using a class derived from TreeNode. My class is the same as a tree node but it has a key property I can use to index into collections of other stuff class TreeNodeCommand Inherits TreeNode Public Key as string End Class In order to make my code slick I then wrote two conversion functions, so that any nodes returned from treeview are converted to TreeNodeCommand... Private Function CurrentNode() As TreeNodeCommand Return ToTreeNodeCommand(tvwCommands.SelectedNode) End Function Private Function RootNode() As TreeNodeCommand Return ToTreeNodeCommand(tvwCommands.Nodes.Item(0)) End Function ' Convert tree node item to tree node command item Private Function ToTreeNodeCommand(ByRef prItem As TreeNode) As TreeNodeCommand Return CType(prItem, TreeNodeCommand) End Function OK, thats the background. I have a treeview that refers to TreeNodeCommands rather than TreeNodes. I have drag and drop support on this treeview, but I dont want the user to be able to drop a particular thing on the Root node. So in my dragover code I select the node in the treeview under the mouse and using my CurrentNode and RootNode properties I check to see if they are the same node.... If Not CurrentNode Is RootNode Then e.Effect = DragDropEffects.Copy End If ...but guess what. Every single time the IF statement executes, I get another node added to the tree, which is effectively a CLONE of the root node. If I do this, which is perfectly acceptable, but not as slick to read then all is well... If Not tvwCommands.SelectedNode Is tvwCommands.Nodes(0) Then e.Effect = DragDropEffects.Copy End If You will have had to see this to believe it. No where does any single line of code get executed that does a ...Nodes.Add. Yet, new nodes do appear. I can work around this, but I would be really interested to see if for some reason you think the Clone method of the root node is being invoked by the type conversion, or by some other means. I think that's what it is. The interesting thing is I override the Clone method in my derived class and it doesn't get called at any time. So, I am lost in la la land with this one people. Any thoughts or similar experiences would be appreciated. Perhaps I am missing something due to my lack of .NET experience. Apologies for the lengthy post.
It does seem that the root node is being duplicated by the tree. Here is another case... ' This function reads nodes and adds things to another collection NodesToCommands(DirectCast(tvwCommands.Nodes(0), TreeNode), _ CommandList.SingletonObject.RootCommandTree) ... The first parameter tvwCommands.Nodes(0) normally returns TreeNode. Please remember, and forgive my repetition, but in my case it returns a TreeNodeCommand object. If I had the DirectCast around this parameter all is well. If I do not then the tree from the root node down is duplicated and added back to the tree! Each time I can find work arounds to this stuff, but why the hell is it happening? Nursey
-
It does seem that the root node is being duplicated by the tree. Here is another case... ' This function reads nodes and adds things to another collection NodesToCommands(DirectCast(tvwCommands.Nodes(0), TreeNode), _ CommandList.SingletonObject.RootCommandTree) ... The first parameter tvwCommands.Nodes(0) normally returns TreeNode. Please remember, and forgive my repetition, but in my case it returns a TreeNodeCommand object. If I had the DirectCast around this parameter all is well. If I do not then the tree from the root node down is duplicated and added back to the tree! Each time I can find work arounds to this stuff, but why the hell is it happening? Nursey
Try this. To access the RootNode tvwCommands.TopNode :) Free your mind...
-
Try this. To access the RootNode tvwCommands.TopNode :) Free your mind...