0
votes

VS C# Winform treeview: I'm a beginner and appreciate the help.

I want to uncheck boxes of previous selected node. The application only allows the user to select a node with children. They can then check child nodes. If they then select another parent node the previous check boxes need to be cleared.

Parent1 has child nodes A, B, & C Parent2 has child nodes X, Y, & Z If Parent 1 is selected and A, B, & C are checked and the user then selects Parent2, the checkboxes of A, B, & C of Parent 1 need to be cleared.

I've tried catching the SelectedNode in the beforeSelect event with TreeNode tnBeforeSelect = tvFileMan.SelectedNode; but when I try to use it in the after select event it doesn't exist in that context. I have a global variables class but could not figure out how to add a treenode variable. I thought I could then uncheck in the afterCheck event with something like.

foreach (TreeNode tn in tnBeforeSelect)  
{                            
      tnBeforeSelect.Nodes.Checked = false;
}
1
Can't u just clear the nodes checkboxes, other than the one is selected in a foreach loop?Alex Jolig
Alex, that's what I'm trying to do. The question is what would that code look like? Specifically, how do I reference the node and children I want to uncheck since they are no longer selected?Thomas Ash

1 Answers

0
votes

Resolved. Location, Location, Location. The trick was to place the foreach loop in the BeforeCheck event handler since SelectedNode hasn't changed yet.

            if (e.Node.Nodes.Count >= 1)
            {
                if (tvFileMan.SelectedNode != null)
                {
                    tvFileMan.SelectedNode.Checked = false;
                    foreach (TreeNode tn in tvFileMan.SelectedNode.Nodes)
                    {
                        if (tn.Nodes.Count.Equals(0))
                            tn.Checked = false;
                    }
                }
            }