如果检查了子项,则检查父项

本文关键字:检查 如果 | 更新日期: 2023-09-27 18:28:37

我得到了这个树视图,如果选中了parent,我就可以选择所有的子级,同样也是如此。还有另一条规则可以检查一些受抚养的孩子。问题是:如果有孩子被检查了,我想检查家长,但因为其他规则,我找不到一种不与规则冲突的方法。这是我迄今为止制作的代码:

    private void tvMorgan_AfterCheck(object sender, TreeViewEventArgs e)
    {
        //Check Children if parent checked
        if (e.Node.Nodes.Count > 0)
        {
            TreeNode tnParent = e.Node;
            if (tnParent.Checked)
            {
                foreach (TreeNode tnChild in tnParent.Nodes)
                {
                    tnChild.Checked = true;
                }
            }
            //Unchecked children if parent unchecked
            else
            {
                foreach (TreeNode tnChild in tnParent.Nodes)
                {
                    tnChild.Checked = false;
                }
            }
        }
        //If dependent node is selected, check the other two
        else if (((e.Node.Text.Contains("BRL/EUR")) && (e.Node.Checked)) && (e.Node.Parent.Text.Contains("FWD")))
        {
            TreeNode tnParent = e.Node.Parent;
            foreach (TreeNode tn in tnParent.Nodes)
            {
                if (tn.Text.Contains("BRL/USD") || tn.Text.Contains("EUR/USD"))
                    tn.Checked = true;
            }
        }
        //If one of the two necessary nodes are uncheked, then uncheck the dependent one
        else if ((((e.Node.Text.Contains("BRL/USD")) || (e.Node.Text.Contains("EUR/USD"))) && (!e.Node.Checked)) && (e.Node.Parent.Text.Contains("FWD")))
        {
            TreeNode tnParent = e.Node.Parent;
            foreach (TreeNode tn in tnParent.Nodes)
            {
                if (tn.Text.Contains("BRL/EUR"))
                    tn.Checked = false;
            }
        }
    }

提前感谢

如果检查了子项,则检查父项

TreeView.AfterCheck事件的文档准确地显示了如何执行您正在查找的