Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# Programatically expanding a node in a TreeView .Net 2.0

Options
  • 05-12-2012 5:38pm
    #1
    Registered Users Posts: 7,501 ✭✭✭


    I have a treeview which has a list of users.
    After the user edits some information about that user and presses save i repopulate the treeview.

    I would like to get back to the user that was being edited.

    However when i try and use the below to achieve this it doesnt work.
    
    TreeNode currentNode = this.treeView1.selectedNode;
    
    // Here i repopulate the treeview.
    
    gotoLastLocation(currentNode);
    
    private void gotoLastLocation(TreeNode tn)
    {
    	this.treeView1.SelectedNode = tn;
    }
    

    This however doesnt work. Nothing happens to the TreeView. Nothing expands.

    If i check the tn.FullPath property i get the following:

    System.InvalidOperationException: Full path can only be retrieved when a TreeNode has been added to a TreeView. This TreeNode has not been added to a TreeView.

    EDIT: If i check the currentNode.FullPath before repopulating the treeview it works fine. If i check it after repopulating the list it gives the above error.
    I cant seem to figure this out. Any ideas?


Comments

  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    When you repopulate the treeview you are essentially destroying all the nodes in it. Your reference to currentNode keeps a copy of the node object, but it's no longer a part of the treeviews node collection. So it's no longer connected to the treeview, and it's no longer a reference to the corresponding new node in the treeview.

    You'll need to find some other way to track what the current selected node should be (tag, label etc), then find the new node that matches that.


  • Registered Users Posts: 2,494 ✭✭✭kayos


    When you repopulate the tree view are you creating new node objects? If so then the selected node object will not exist in the new treeview as your error is telling you.


  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    Ah ok. Ya im basically creating everything again.

    So if i save the FullPath string instead is there a way to go to a node based on the FullPath string or will i essentially have to do a recursive loop through all the nodes until i find what im looking for?


  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    TreeNodeCollection.Find seems to be what im looking for but i cant seem to find out what the KEY string is.

    Is it the .Tag, .Text or what of the node?


  • Registered Users Posts: 2,494 ✭✭✭kayos


    Question for you...Do you really need to reload the complete tree when you have just edited the properties of a single node? That is like printing out that 1000page document again because you fixed a typo on page 8 i.e. wasteful of resources. Now of course if you add a new page or resort the chapters you might want to print it all out again but for a simple typo fix you would just print that page out again.

    In other words save the changes, read the data for that node back and then update the node in question. Leave the rest of the tree alone.


  • Advertisement
  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    kayos wrote: »
    Question for you...Do you really need to reload the complete tree when you have just edited the properties of a single node? That is like printing out that 1000page document again because you fixed a typo on page 8 i.e. wasteful of resources. Now of course if you add a new page or resort the chapters you might want to print it all out again but for a simple typo fix you would just print that page out again.

    In other words save the changes, read the data for that node back and then update the node in question. Leave the rest of the tree alone.

    Im editing a preexisting project. It not actually necessary to reload the whole tree but it will require a **** load of work to refactor the whole thing to do it correctly.

    I might end up having to do that however if i cant figure this out.


  • Registered Users Posts: 14,714 ✭✭✭✭Earthhorse


    According to this article (http://msdn.microsoft.com/en-us/library/system.windows.forms.treenodecollection.find%28v=vs.80%29.aspx) the key is just the Name property of the node. So if you just find what's being assigned to Name you should be good to go.

    But rewriting it to just update the relevant node is better if you can find the time.


  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    Earthhorse wrote: »
    According to this article (http://msdn.microsoft.com/en-us/library/system.windows.forms.treenodecollection.find%28v=vs.80%29.aspx) the key is just the Name property of the node. So if you just find what's being assigned to Name you should be good to go.

    But rewriting it to just update the relevant node is better if you can find the time.

    Thanks.

    The muppet who wrote this before me made the name property non unique and doesn't relate to the text property so any search returns loads of results and to make it worse they are using this non unique value in loads of places so I can't change it.

    Looks like I'm gona have to rewrite the updating method. Arrrgh!!


  • Registered Users Posts: 2,494 ✭✭✭kayos


    Im editing a preexisting project. It not actually necessary to reload the whole tree but it will require a **** load of work to refactor the whole thing to do it correctly.

    Is it? Then you really need to do it...think of the next guy who looks at the code in 6months time.

    I've seen some horrible code revolving around tree views (one 1600 line method comes to mind) just because someone else wrote it and its a big re-factor does not mean you should avoid putting things right.


Advertisement