how to check the node's text, level and depth in a treeview control?

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.




Answer this question

how to check the node's text, level and depth in a treeview control?

  • Doug Hall

    but how will I know that the node is the right parent can i iterate through all the nodes and perform a test: if (node.Text == "Mama") { son = me; }

  • hjs

    got it, i was using System.Collections.Generic, I changed it to System.Collections

  • cmohr

    icemart525 wrote:

    is using tags faster than iterating thru the nodes if it is then how can i set the tags for every added treenode



    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 );




  • PICASSO3

    icemart525 wrote:
    but how will I know that the node is the right parent can i iterate through all the nodes and perform a test: if (node.Text == "Mama") { son = me; }


    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.


  • Langdon White

    Chris Vega wrote:

    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

    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



  • ah64apacheip

    is using tags faster than iterating thru the nodes if it is then how can i set the tags for every added treenode



  • Seybaa

    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



  • Dennis Heimbigner

    The child nodes that you add in run-time are also added to there Parent TreeNode. You can allways iterate over the nodes and add new TreeNodes wherever you want.

    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 );
        }
    }

     



  • stec00

    I'm not going to give you the code, but you need to make two recursive functions. One that goes though the checks all the nodes and sees if they have the IsExpanded property set to true and if they do add them to a List<string> object using the nodes .Name as the string key and then you serialize it to disk. Then reload the treeview and the List<string> from disk and after you are done loading it up use another function that recursively goes though all the nodes and looks at the List<string> object and sees if the nodes key matches one of the expanded node keys you saved in the List<string> earlier and if its in there you can do node.Expand() on the node. Make sure you set each nodes name to its fullpath propery, that will make the names unique.
  • T44G44

    thanks PJ. and Chris!

  • how to check the node's text, level and depth in a treeview control?