Saravanan
Saravanan

Reputation: 11612

how to get the All the Checkbox values in TreeView control in C#.net(2010) Windows Forms Application?

I have one Tree View Control with check boxes in my Windows Forms Application.Whenever user selected multiple Checkboxes, then i want to display the all the selected checkboxes node's path.How i do it?

And also How to automatically select or deselect the all child nodes whenever its parent Node is selected or deselected?

Upvotes: 0

Views: 2620

Answers (1)

TBohnen.jnr
TBohnen.jnr

Reputation: 5129

protected string getCheckedNodes(TreeNodeCollection tnc)
    {
        StringBuilder sb = new StringBuilder();

        foreach (TreeNode tn in tnc)
        {
            if (tn.Checked)
            {
                string res = tn.FullPath;
                if (res.Length > 0)
                    sb.AppendLine(res);
            }
            string childRes = getCheckedNodes(tn.Nodes);
            if (childRes.Length > 0)
                sb.AppendLine(childRes);
        }

        return sb.ToString();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(getCheckedNodes(treeView1.Nodes));
    }

I've made it output via string but you can obviously do anything you like with it like add it to a collection etc.

Upvotes: 2

Related Questions