Tree View Control

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2009
Posts: 22
Reputation: seebharath is an unknown quantity at this point 
Solved Threads: 0
seebharath seebharath is offline Offline
Newbie Poster

Tree View Control

 
0
  #1
Aug 11th, 2009
Hello,

I have a treeview control and want to add a node to it at a desired location or as the child of a Certain Node X.The problem is I will know nodename[or the Text displayed ] of X only at runtime.

How do I Iterate the tree to find the given Node and add a child to it ??

Do I have to write a recursive function that iterates the nodes of a tree,find the given node and add a child to it OR is there an easier way to do this..
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 59
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: Tree View Control

 
0
  #2
Aug 11th, 2009
Based on what information you want to add child nodes?
Can you explain more about why you want to do this, this will help us to provide better solution.
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 954
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 196
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: Tree View Control

 
0
  #3
Aug 11th, 2009
Originally Posted by seebharath View Post
Hello,

I have a treeview control and want to add a node to it at a desired location or as the child of a Certain Node X.The problem is I will know nodename[or the Text displayed ] of X only at runtime.

How do I Iterate the tree to find the given Node and add a child to it ??

Do I have to write a recursive function that iterates the nodes of a tree,find the given node and add a child to it OR is there an easier way to do this..
Use the TreeView's TreeNodeCollection Find method:
public TreeNode[] Find(string key, bool searchAllChildren);

Then, peruse the returned array for your specific node and Add your new node.
Last edited by DdoubleD; Aug 11th, 2009 at 11:37 am. Reason: spelling
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 954
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 196
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: Tree View Control

 
0
  #4
Aug 11th, 2009
If you are not actually using the TreeNode.Name (was unclear), you will need to use recursion on the TreeNode.Text. Let me know if you need help with that.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: seebharath is an unknown quantity at this point 
Solved Threads: 0
seebharath seebharath is offline Offline
Newbie Poster

Re: Tree View Control

 
0
  #5
Aug 11th, 2009
Hai,
Thanks for the Quick response..
If I do TreeView's TreeNodeCollection Find method it will return me a treenodecollection and any changes I do will not be reflected in the actula treeview.Please correct me if I am wrong..
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: seebharath is an unknown quantity at this point 
Solved Threads: 0
seebharath seebharath is offline Offline
Newbie Poster

Re: Tree View Control

 
0
  #6
Aug 11th, 2009
Hai..
IF I am using Treenode.Name then how can I proceed to search for a given Node and append a child to it..??
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: seebharath is an unknown quantity at this point 
Solved Threads: 0
seebharath seebharath is offline Offline
Newbie Poster

Re: Tree View Control

 
0
  #7
Aug 11th, 2009
Hai,
@DangerDev
I get a input from the user and based on this input there will be corresponding node in my tree view(Can be at any level inside the treeview),and I will know only its name .Have to find this node and append my child node to it..
Where I add my child Node depends on the user and has to be done at runtime..
Hope this Helps..
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 954
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 196
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: Tree View Control

 
0
  #8
Aug 11th, 2009
Originally Posted by seebharath View Post
Hai,
Thanks for the Quick response..
If I do TreeView's TreeNodeCollection Find method it will return me a treenodecollection and any changes I do will not be reflected in the actula treeview.Please correct me if I am wrong..
It will return a reference to the node collection, so your changes will apply.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 954
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 196
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: Tree View Control

 
0
  #9
Aug 11th, 2009
Originally Posted by seebharath View Post
Hai,
@DangerDev
I get a input from the user and based on this input there will be corresponding node in my tree view(Can be at any level inside the treeview),and I will know only its name .Have to find this node and append my child node to it..
Where I add my child Node depends on the user and has to be done at runtime..
Hope this Helps..
If you are searching on the TreeNode text, you can use the following recursion:
[CODE]
public static TreeNode FindTreeNodeText(TreeNodeCollection nodes, string findText)
{
TreeNode foundNode = null;
for (int i = 0; i < nodes.Count && foundNode == null; i++)
{
if (nodes[i].Text == findText)
{
foundNode = nodes[i];
break;
}
if (nodes[i].Nodes.Count > 0)
foundNode = FindTreeNodeText(nodes[i].Nodes, findText);
}
return foundNode;
}
[\CODE]
The node collection is the TreeView's Nodes (tv.Nodes). If found, you simply foundNode.Add(new TreeNode());
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 954
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 196
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: Tree View Control

 
0
  #10
Aug 11th, 2009
Reposting method: I don't know why the tags didn't display the code properly. If this fixes your problem, please flag as Solved--thanx.
  1. public static TreeNode FindTreeNodeText(TreeNodeCollection nodes, string findText)
  2. {
  3. TreeNode foundNode = null;
  4. for (int i = 0; i < nodes.Count && foundNode == null; i++)
  5. {
  6. if (nodes[i].Text == findText)
  7. {
  8. foundNode = nodes[i];
  9. break;
  10. }
  11. if (nodes[i].Nodes.Count > 0)
  12. foundNode = FindTreeNodeText(nodes[i].Nodes, findText);
  13. }
  14. return foundNode;
  15. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC