-
December 15th, 2011, 11:31 AM
#1
[RESOLVED] TreeView - add new nodes and subnodes if not existing
Hi, I have a few classes that should report stuff to a TreeView. Some of the classes are single instanced and other multiple. So, for the single instanced classes I'll have just one root node to store its info, and for the multiple instanced, I want to have a root node for the class category and then multiple nodes as new instances get created. Something like:
Dog (single)
- info for dog
Cats (multiple)
- Cat1
-- info for cat 1
- Cat 2
-- info for cat 2
The Nodes hierarchy is given by a string[] containing all the names. So, string[0] would be the root node, string[1] would be a child of string[0], string[2] would be a child of string[1], etc.
I'm having a hard time creating the nodes and then finding them when I have to add something to one of them.
When adding, I want to check if there is an existing one already, and only add if not.
Code:
string[] treeNames = { "House 1", "Dogs", "Fluffy" }
if (treeview doesn't have a node "House 1")
add it;
if (now new or already existing node "House 1" doesn't have a node "Dogs")
add it;
if (now new or already node "Dogs" doesn't have a node "Fluffy")
add it;
it all would point to a foreach loop with the string[], but I can't seem to find an easy way of getting a reference to the newly created nodes to add subnodes to them.
I tried this, and it adds the root nodes, but not the child nodes.
Code:
string rootNodeName = treeNames[0];
TreeNode newNode, lastAddedNode = null;
foreach (string branch in treeNames)
{
newNode = new TreeNode(branch);
if (lastAddedNode == null)
{
// Root Node, added to the TreeView
TreeNode[] existingNodes = this.tvDebugInformation.Nodes.Find(branch, false);
if (existingNodes.GetLength(0) == 0)
this.tvDebugInformation.Nodes.Add(branch);
}
else
{
// Child Nodes, added to previously added TreeNode
TreeNode[] existingNodes = lastAddedNode.Nodes.Find(branch, false);
if (existingNodes.GetLength(0) == 0)
lastAddedNode.Nodes.Add(branch);
}
lastAddedNode = newNode;
}
The final step is then finding the child node I want when adding info.
A simple treeview.Nodes.Find(<name>, true) shold serve, but I think it's not working very well or I have messed something. This part should be easy once the adding and sorting is done properly. When trying to find a Node, I will have the entire tree again, so I could start searching from the top one by one.
It looks like it's fairly easy to do, but I can't get it!
Any help appreciated!
-
December 16th, 2011, 03:56 PM
#2
Re: TreeView - add new nodes and subnodes if not existing
Not sure if it's the best solution, but it works. The reason why Find() was not finding the newly added Nodes is that Find searches by key and not by name. The <new TreeNode(string name)> Constructor does not auto assign the name as key, so I'd strongly recommend using <new TreeNode(string key, string name)>, which will give non-empty results when searching.
In my case, the (almost) only change needed was
Code:
this.tvDebugInformation.Nodes.Add(branch, branch);
Hope it helps anyone eventually =)
-
April 26th, 2013, 01:09 PM
#3
Re: [RESOLVED] TreeView - add new nodes and subnodes if not existing
Thanks very much. I have an application that allows auditing of Active Directory. It has a tree of AD on the left and when a person was selected through global search I wanted to expand the AD tree all the way to the selected user and then highlight the selected user.
I almost gave up. I finally came back to the code and researched via Google and I finally found your post.Treeview.nodes.find simply did not work. After reading your post I modified my code that builds the tree to treeview.nodes.add("nodename", "Nodename"). After adding the key my .Find code works great and I now expand the tree as desired.
I did a lot of research and could not find the answer until your post. I don't know how you figured this out but I thank you!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|