Skip to content

TreeView Control help in C#

Sree

Asked Nov 3, 2005, 7:58 AM EST1 answer
Share
LinkedInX
Hello,

Need a help from the forum..

I have a TreeView Control in my windows.forms coded in C#.I have a few parent nodes and each parent node as some child nodes. I wanted to get the number of Checked parents with their checked child nodes. Can you pls help me how do I get this ?

thanks,
Sree.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi ,

    You can use the following code snippet to get the Checked Parent nodes and child nodes:

    private void button1_Click(object sender, System.EventArgs e)
    {
     Recursion(this.treeView1.Nodes);
     this.label1.Text="Parent Nodes : "+ParentNodes+"\n"+"Child Nodes :"+ChildNodes;
     ParentNodes=0;
     ChildNodes=0;
    }

    int ParentNodes=0,ChildNodes=0;

    public void Recursion(TreeNodeCollection nodes)
    {
    if(nodes==null)
     return;
    foreach(TreeNode node in nodes)
    {
    if(node.Nodes.Count>0 && node.Checked)
    {
     ParentNodes++;
     Recursion(node.Nodes);
    }
    else if(node.Checked)
    {
     ChildNodes++;
    }
    else if(node.Nodes.Count>0)
     Recursion(node.Nodes);
    }
    }

    I cant upload my test sample in this forum. So if you need that sample ask me to this mail id: [email protected].

    let me know if you have any further question.

    Thanks,
    Vijay

    0