Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically adding Nodes to a TreeView

Status
Not open for further replies.

SlantyOD

Programmer
Jun 11, 2002
22
0
0
CA
Hi all,

I'm working on a program that tests the API of another program, and then assigns a pass or fail grade to each test. Because there are a large number of tests I have most of the work occurring on a separate thread from the main form, and use a delegate to update a treeview on the main form. The problem I'm having is that I wish to add these nodes one at a time, but if I do so I get duplicates of the root node. Ie:

TestGroup1
-Test1
TestGroup1
-Test2
TestGroup2
-Test1
etc

Can anybody help me with some idea as to how to test that the parent node has already been created and if so then add the child node to that node?
 
I actually worked it out. Here's my code, feel free to point out any errors =).

if (myTreeVew.Nodes.Count > 0)
{
foreach(TreeNode tn in myTreeView.Nodes)
{
if (tn.Text == NodeToBeAdded.Text)
{
tn.Nodes.Add(NodeToBeAdded.FirstNode.Text)
}
else
{
myTreeView.Nodes.Add(NodeToBeAdded)
}
}
}
else
{
myTreeView.Nodes.Add(NodeToBeAdded)
}

I can get away with using FirstNode as I'm only passing one node at a time. At this point it should be a small matter to make it recursive, but I thought I'd put it here as I'm still very new at working with TreeNodes (and C# in general) so please feel free to question/comment/point and laugh =).

Cheers,
SlantyOD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top