带有CheckBoxes的TreeView在c#中

时间:2011-03-29 21:12:06

标签: c# treeview

我在c#中有一个带有复选框的树视图,我希望当用户检查一个节点时,自动检查下面级别上的所有节点。 每次用户检查某个节点时,有没有人知道如何在所有树上运行recorsive fnction?

由于

//此函数返回treeView。

   public TreeView GetTreeView()
    {

        getSubject();
        // fill the treeview with all subjects.
        foreach (Subject subject in subjects)
        {
            //for each root subject fill all the his children.
            if (subject.subjestId == subject.parentSubject)
            {
                TreeNode node = new TreeNode(subject.subjectString, subject.subjestId, subject.subjestId);
                addChild(node, subject.subjestId);
                tv.Nodes.Add(node);
            }
        }
        return tv;
    }
   // for each subject return sub subjects.
   private void addChild(TreeNode node, int parentId)
    {
        foreach (Subject subject in subjects)
        {
            if (subject.parentSubject == parentId && subject.parentSubject != subject.subjestId)
            {
                TreeNode childNode = new TreeNode(subject.subjectString, subject.subjestId, subject.subjestId);
                addChild(childNode, subject.subjestId);
                node.Nodes.Add(childNode);
            }
        }
    }

5 个答案:

答案 0 :(得分:17)

递归。像这样:

    bool busy = false;

    private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) {
        if (busy) return;
        busy = true;
        try {
            checkNodes(e.Node, e.Node.Checked);
        }
        finally {
            busy = false;
        }
    }

    private void checkNodes(TreeNode node, bool check) {
        foreach (TreeNode child in node.Nodes) {
            child.Checked = check;
            checkNodes(child, check);
        }

答案 1 :(得分:1)

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) {
  foreach (TreeNode child in e.Node.Nodes) {
    child.Checked = e.Node.Checked;
  }
}

答案 2 :(得分:0)

这是一个更好的解决方案

private void trvMenuList_AfterCheck(object sender, TreeViewEventArgs e)
        {
            SetChildrenChecked(e.Node, e.Node.Checked);

        }

  private void SetChildrenChecked(TreeNode treeNode, bool checkedState)
        {
            foreach (TreeNode item in treeNode.Nodes)
            {
                if (item.Checked != checkedState)
                {
                    item.Checked = checkedState;
                }
                SetChildrenChecked(item, item.Checked);
            }
        }

答案 3 :(得分:0)

当一些答案陈述时,创建一个递归的'set checked to children'函数,然后在树上调用它AfterCheck。

不幸的是,即使您在代码中设置了检查值,该框架也会给您回调AfterCheck,尽管在小树中这可能不明显,但为您的应用添加了大量的指数性额外工作。为避免这种情况,请过滤AfterCheck以仅在用户触发新功能时触发它。

    private void tree_AfterCheck(object sender, TreeViewEventArgs e)
    {
        if (e.Action != TreeViewAction.Unknown)
        {
            SetChildrenChecked(e.Node);
        }
    }

    private void SetChildrenChecked(TreeNode treeNode)
    {
        foreach (TreeNode item in treeNode.Nodes)
        {
            if (item.Checked != treeNode.Checked)
            {
                item.Checked = treeNode.Checked;
            }

            if (item.Nodes.Count > 0)
            {
                SetChildrenChecked(item);
            }                
        }
    }

答案 4 :(得分:-1)

如果你想在WinForms中这样做,那么我认为你必须通过递归手动完成它 - 我不知道更好的方法。