Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Treeview adds nodes to itself without apparent cause

Treeview adds nodes to itself without apparent cause

Scheduled Pinned Locked Moved Visual Basic
csharpdatabasedata-structuresdiscussion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cnurse
    wrote on last edited by
    #1

    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.

    C 1 Reply Last reply
    0
    • C cnurse

      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.

      C Offline
      C Offline
      cnurse
      wrote on last edited by
      #2

      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

      G 1 Reply Last reply
      0
      • C cnurse

        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

        G Offline
        G Offline
        Guillermo Rivero
        wrote on last edited by
        #3

        Try this. To access the RootNode tvwCommands.TopNode :) Free your mind...

        C 1 Reply Last reply
        0
        • G Guillermo Rivero

          Try this. To access the RootNode tvwCommands.TopNode :) Free your mind...

          C Offline
          C Offline
          cnurse
          wrote on last edited by
          #4

          Hi Guille, Yeh but I would still cast TopNode to a TreeNodeCommand see? The problem seems to be casting the node causes the tree to duplicate itself, get me? Nursey

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups