none
Get inner nodes properties RRS feed

  • Question

  • Hi,
    i want to put a loop that on all nodes ( parent and child ) repeat so i can get their texts or their name !
    i used below link but when loop start's it will only get the main nodes ( i mean the nodes that are created first ) but it wont enter the inner nodes what should i do to enter the inner nodes? !
    Thanks.

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control

    Friday, January 26, 2018 7:58 PM

Answers

  • The loop in the example that you linked only gets one level of nodes, but then inside the loop it calls recursively the same function to descend to each inner level of nodes. If you are not getting all nodes, you probably forgot the recursive call when you transcribed the code from the example.
    Friday, January 26, 2018 9:25 PM
    Moderator
  • Hello Armin,

    The link you provided will iterate all nodes, of course it will get all inner nodes. For a given treeview.

    public Form1()
            {
                InitializeComponent();
    
                treeView1.Nodes.Add("Root", "Root Text");
                treeView1.Nodes["Root"].Nodes.Add("Child1", "Child1 Text");
                treeView1.Nodes["Root"].Nodes.Add("Child2", "Child2 Text");
                treeView1.Nodes["Root"].Nodes["Child2"].Nodes.Add("SubChild","SubChild1 Text");
            }

    Test code.

           private void PrintRecursive(TreeNode treeNode)
            {
                MessageBox.Show(treeNode.Text);
                // Print each node recursively.  
                foreach (TreeNode tn in treeNode.Nodes)
                {
                    PrintRecursive(tn);
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                
                PrintRecursive(treeView1.SelectedNode);
            }

    In my example I use treeView1.SelectedNode to specify the parent node that will iterate, you also could use treeView1.Nodes["Root"].

    Result.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 28, 2018 9:15 AM
    Moderator

All replies

  • The loop in the example that you linked only gets one level of nodes, but then inside the loop it calls recursively the same function to descend to each inner level of nodes. If you are not getting all nodes, you probably forgot the recursive call when you transcribed the code from the example.
    Friday, January 26, 2018 9:25 PM
    Moderator
  • Hello Armin,

    The link you provided will iterate all nodes, of course it will get all inner nodes. For a given treeview.

    public Form1()
            {
                InitializeComponent();
    
                treeView1.Nodes.Add("Root", "Root Text");
                treeView1.Nodes["Root"].Nodes.Add("Child1", "Child1 Text");
                treeView1.Nodes["Root"].Nodes.Add("Child2", "Child2 Text");
                treeView1.Nodes["Root"].Nodes["Child2"].Nodes.Add("SubChild","SubChild1 Text");
            }

    Test code.

           private void PrintRecursive(TreeNode treeNode)
            {
                MessageBox.Show(treeNode.Text);
                // Print each node recursively.  
                foreach (TreeNode tn in treeNode.Nodes)
                {
                    PrintRecursive(tn);
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                
                PrintRecursive(treeView1.SelectedNode);
            }

    In my example I use treeView1.SelectedNode to specify the parent node that will iterate, you also could use treeView1.Nodes["Root"].

    Result.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 28, 2018 9:15 AM
    Moderator