Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 25th, 2005, 10:27 AM
Registered User
 
Join Date: Nov 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Focusing on a node in a treeview (C# application)

Hi,

I want to focus on a specific node in a TreeView. This should be based on either a string or an index value (or something). The thing is, I am adding a node to my TreeView. Each time I do that the tree has to be reBuilt, in order to show the new node. This tree is quite big, so I have to scroll quite a bit to get to where I was. Therefore I would like to somehow save an index or string (based on the node i am currently at), and then return to it. Is this possible?

thnx

 
Old November 25th, 2005, 02:15 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Use the SelectedNode property of TreeView control to get the selected TreeNode before adding the new TreeNode. After you are done with adding the new TreeNode set the SelectedNode property of TreeView control back to the previously selected TreeNode.

Regards
Ankur Verma
 
Old November 25th, 2005, 02:51 PM
Registered User
 
Join Date: Nov 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi thanks 4 your reply :)

The thing is though... When I add a node, what I actually do is to make an insert into a table, then I have to call a method, mainWindow.buildTreeMenu(). This method calls a db class to populate the TreeView. Therefore, the whole TreeView is "cleared" and then rebuilt every time i add a node (make an insert). So... I know I can save the selectedNode, but what do I do with it later...? The TreeView is cleared and rebuilt, that's kind of a problem. Any ideas?

thnx

 
Old November 25th, 2005, 05:34 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Well working with tree views would have been much easier if there was a way of locating the nodes based on their paths. I've searched around a bit from time to time but surprisingly there is no such method or function or a way with which you could do that. Or should I say- I couldn’t find anything significant in this regard.

So here's a step, a function that would locate the nodes based on the paths provided

        TreeNode LocateNode(string Path, TreeNodeCollection TreeCol)
        {
            string[] PTms = Path.Split(new Char[] {'\\'});

            for(int k = 0; k < TreeCol.Count;k++)
            {
                if(TreeCol[k].Text == PTms[0])
                {
                    if(TreeCol[k].Nodes.Count == 0 || PTms.Length == 1)
                        return TreeCol[k];

                    return LocateNode(Path.Remove(0,PTms[0].Length+1), TreeCol[k].Nodes);
                }
            }
            return null;
        }


I wrote this function to accept TreeNodeCollection, it was easier to write a recursive function that way, so you have to pass it the relevant TreeView's Nodes property.

Now what you can do is - save the FullPath property of the currently selected node. Once you are done with the adding, locate the node in the new tree with the saved FullPath, set it to the SelectedNode property of the TreeView.
That is case one.
Case Two would be if the contents of the tree are changing a bit too much or in such a way that u can no longer trust FullPath for leading you to the right node, in which case you will have to create and maintain the path separately for the node you want selected post insertion while the insertion process is on.

Once addition is over, use this path to locate the TreeNode and set it to the SelectedNode property.

I hope you find something useful out of it.

Disclaimer: This function is provided as is. I do not hold any guarantees if anything gets messed up using this function.

Don’t get me wrong but U have to write such stuff.



Regards
Ankur Verma
 
Old November 26th, 2005, 01:55 PM
Registered User
 
Join Date: Nov 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much :D

You are quite a "coder"!

 
Old November 27th, 2005, 09:58 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

You are welcome.
If coding helps anyone, coding is worth it and i feel learning how to code was worth its while. Just hope I was able to help.

Regards
Ankur Verma





Similar Threads
Thread Thread Starter Forum Replies Last Post
TreeView and SiteMap Node Order robzyc ASP.NET 1.0 and 1.1 Basics 0 May 19th, 2008 04:27 AM
How to Drag and Drop node from treeview to Datagri mohan_here C# 1 April 26th, 2007 12:41 PM
How to select a node or text from treeview MacDevv C# 2005 3 September 5th, 2006 05:23 AM
Add node in ie treeview acko ASP.NET 1.x and 2.0 Application Design 0 January 28th, 2004 09:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.