I am using treeview with checkboxes in each node as shown below:
+[]Main
[]Child1
[]Child2

I have an indicator from form1 which is checkbox1 and checkbox2 that is uses to control the checkboxes of child1 and 2. If checkbox1 is true automatically child1 will be true, and if checkbox2 is true, child 2 will be true likewise, and so on.

So it hard for me to manipulate the child nodes using the indicator, i need help for this problem coz i am only new to c#, can you show me the code for this problem?

My 2nd question is that, Is there a way to remove the checkbox at Main node coz it is not necessary to have a checkbox there?

thanks in advance and god bless!

Hardz

Recommended Answers

All 2 Replies

Assuming that "checkbox1 and checkbox2" are CheckBox controls on you form you can use the Tag property to link the TreeNode and the CheckBox control.

When you add the TreeNode

// get root node
            TreeNode rootNode = treeView1.Nodes[0];
            // create new tree node
            TreeNode tn = new TreeNode("Child1");
            // Save CheckBox control pointer in TreeNode Tag property
            tn.Tag = checkBox1;
            // Save TreeNode pointer in CheckBox control Tag property
            checkBox1.Tag = tn;
            // add new node to root node
            rootNode.Nodes.Add(tn);

Event handler for TreeView.AfterCheck

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag is CheckBox)
            {
                ((CheckBox)e.Node.Tag).Checked = e.Node.Checked;
            }
        }

Event handler for check boxes (set each CheckBox to point to same handler)

private void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            if (cb != null)
            {
                if (cb.Tag is TreeNode)
                {
                    ((TreeNode)cb.Tag).Checked = cb.Checked;
                }
            }
        }

Ok thank you very very much sir my problem was solved...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.