首页 > 代码库 > TreeView只能选中一个节点

TreeView只能选中一个节点

让TreeView的所有节点包括父节点和子节点,有且仅有一个能被选中:

        private void checkAllNodes(TreeNode pNode,bool nodeChecked,TreeNodeCollection col)
        {
            if (nodeChecked == true)
            {
                foreach (TreeNode node in col)
                {
                    if (node != pNode)
                    {
                        node.Checked = !nodeChecked;
                    }
                    if (node.Nodes.Count > 0)
                    {
                        // If the current node has child nodes, call the checkAllNodes method recursively.
                        this.checkAllNodes(pNode,nodeChecked, node.Nodes);
                    }
                   
                }
            }
        }

 

调用: this.checkAllNodes(e.Node,e.Node.Checked, treeView1.Nodes);

TreeView只能选中一个节点