Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use tree view in my window application in c#.
I have Root not named Hart and it's child is serial and serial's child is COM.
and there is many child node at COM.
I want all COM's child name in one array i add child dynamically at run time.
I want to search COM child node and if child is already with same name which i want to add then don't want to add if not then add.
I use this code:

C#
TreeNode[] treeCol = COM.Nodes.Find(no.ToString(), true);
if (treeCol.Length > 0)
{
      Not add....
}
else
{
    TreeNode po1 = COM.Nodes.Add(no.ToString());
}

This code add repeat child so this will not work as i want.


[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
Posted
Updated 7-Jan-12 0:42am
v3

I think TreeNode.ToString() does not return the node Name or Text. Try using no.Name.

You must add nodes using TreeNode.Add(key, text), otherwise Node.Name is empty
 
Share this answer
 
v3
Comments
jaideepsinh 7-Jan-12 9:07am    
no.Name Does't working.
jaideepsinh 10-Jan-12 6:46am    
Can you explain this code in brief i don't understand this.
Catalin Serafimescu 10-Jan-12 6:54am    
TreeNode.Find needs a key. You are adding nodes with text only, so key is empty.
Just give it a try.
bool repeatlivenode = false;
foreach (TreeNode tn in po.Nodes)
{
if (tn.Text == ti)//ti is node which i want to add
{
repeatlivenode = true;
break;
}
else
{
repeatlivenode = false;
}
}
if (repeatlivenode == false)
{
TreeNode po1 = po.Nodes.Add(ti);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900