开发者

check and uncheck all the nodes of the tree view in c#

I have the tree view in my windows application and in the tree view with the checkboxes and I have the Some "parent nodes" and some "child nodes" and i want to CHECK AND UNCHECK the parent and child nodes at a time on clicking the "C开发者_JS百科heck All" and "Uncheck All" button... How should i do this?


Try something like this:

public void CheckAllNodes(TreeNodeCollection nodes)
{
    foreach (TreeNode node in nodes)
    {
        node.Checked = true;
        CheckChildren(node, true);
    }
}

public void UncheckAllNodes(TreeNodeCollection nodes)
{
    foreach (TreeNode node in nodes)
    {
        node.Checked = false;
        CheckChildren(node, false);
    }
}

private void CheckChildren(TreeNode rootNode, bool isChecked)
{
    foreach (TreeNode node in rootNode.Nodes)
    {
        CheckChildren(node, isChecked);
        node.Checked = isChecked;
    }
}

Edit: Check branches to tick/untick all child nodes:

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
    CheckChildren(e.Node, e.Node.Checked);
}


Try

private void CheckUncheckTreeNode(TreeNodeCollection trNodeCollection, bool isCheck)
        {
            foreach (TreeNode trNode in trNodeCollection)
            {
                trNode.Checked = isCheck;
                if (trNode.Nodes.Count > 0)
                    CheckUncheckTreeNode(trNode.Nodes, isCheck);
            }
        }

Pass treeView.Nodes to this function like CheckUncheckTreeNode(trView.Nodes, true); in button click event for checking all nodes. To uncheck all do CheckUncheckTreeNode(trView.Nodes, false);.


For ASP.NET WEB APPLICATION:

Button_Click() {
    CheckUncheckTreeNode(YourTreeView.Nodes, false);
}

private void CheckUncheckTreeNode(TreeNodeCollection trNodeCollection, bool isCheck) {
    foreach (TreeNode trNode in trNodeCollection) {
        trNode.Checked = isCheck;
        if (trNode.ChildNodes.Count > 0)
            CheckUncheckTreeNode(trNode.ChildNodes, isCheck);
    }
}


    private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
    {
        if (e.Node.Checked)
        {
            UncheckNodes(treeView1.Nodes,e.Node);
        }

    }

    private void UncheckNodes(TreeNodeCollection nodes, TreeNode except)
    {
        foreach (TreeNode node in nodes)
        {
            if (node != except) node.Checked = false;
            UncheckNodes(node.Nodes, except);
        }
    }


you can use this WITHOUT recursion. put this code in a Button:

foreach (TreeNode Node in TreeNodeInUI.Nodes)
{
    Node.Checked = false;
}


As I stumbled upon a similar issue I found a neat and simple solution. Yes, it's 8 years later, but maybe someone can make use of this.

Whenever a TreeNode is checked, store this TreeNode in a List. When you need to uncheck all nodes, loop the List and uncheck the nodes. This way you only iterate affected nodes.

Example:


Store TreeNodes in a List after check

public List<TreeNode> checkedNodes;

private void treeView1_afterCheck(object sender, TreeViewEventArgs e)
{
    foreach(TreeNode node in treeView1.Nodes)
    {
    if(node.checked) {
        checkedNodes.Add(node);
    }
}

Iterate only checked nodes to uncheck all

public void UncheckAllNodes()
{
    foreach (TreeNode node in checkedNodes)
    {
        node.Checked = false;
    }
}

This is still missing a removal when unchecking a checkbox, but you get the idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜