For example, if I have a parent node Root and then child node Child created at design-time, I know how to add child nodes to Child at run-time but my problem is adding grandchild nodes to Child.
Root
Child
ChildOfChild1 // created at run-time
ChildOfChild2 // created at run-time
How can I add child nodes to ChildOfChild1 or to ChildOfChild2 at run-time the children of Child are sorted randomly so their indices change.

how to check the node's text, level and depth in a treeview control?
Nightmare1980
Waqar Ahmed
Here is a little example that adds a new TreeNode to every root on the deepest depth of the top childnode:
private void button1_Click(object sender, System.EventArgs e)
{
for( int i = 0; i < treeView1.Nodes.Count; i++ )
{
TreeNode lastNode = treeView1.Nodes[ i ];
while( lastNode.Nodes.Count > 0 )
{
lastNode = lastNode.Nodes[0];
}
lastNode.Nodes.Add( "MY NEW TREENODE" );
}
}
Here is a little example that print some properties of all nodes in the TreeView control:
private void button1_Click(object sender, System.EventArgs e)
{
foreach( TreeNode node in treeView1.Nodes )
{
PrintNodeInformation( node );
}
}
public void PrintNodeInformation( TreeNode node )
{
Console.WriteLine( "Text: " + node.Text );
Console.WriteLine( "Path: " + node.FullPath );
Console.WriteLine( "Tag: " + node.Tag );
foreach( TreeNode childNode in node.Nodes )
{
PrintNodeInformation( childNode );
}
}
jmealing
i got an exception
Error 1 Using the generic type 'System.Collections.Generic.Queue<T>' requires '1' type arguments C:\C# Projects\Reporting_System\Reporting_System\frmMain.cs 127 13 Reporting_System
Robert_Hildebrand
is using tags faster than iterating thru the nodes if it is then how can i set the tags for every added treenode
Hr_Ind
fleetfingers
A week ago, I posted code to iterate through TreeView nodes, to search specific node, in this thread: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=326037&SiteID=1
I tested it in searching a node from 10,000 nodes, and it's quite fast. Caching results using HashTable may increase succedding searches.
Regards,
-chris
Vedat Nommaz
It is not faster, but you can store every object you want in the Tag property. The Text property is allways an String type and the Tag property is an Object type.
Here is an example:
TreeNode node = new TreeNode( "MyText" );
node.Tag = tagObject;
treeNode.Nodes.Add( node );
Kristijan M.
Indeed, for example you can use the Text property. But the TreeNode class also has a Tag property that you can use to store information and use it to identify an TreeNode.
farming
saadsal