gpt4 book ai didi

c# - 在 C# 中使用 CheckBoxes 的 TreeView

转载 作者:太空狗 更新时间:2023-10-29 20:53:25 26 4
gpt4 key购买 nike

我在 C# 中有一个带有复选框的 TreeView ,我希望当用户选中一个节点时,自动选中以下级别的所有节点。有谁知道每次用户检查某个节点时无需在所有树上运行 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);
}
}
}

最佳答案

递归。像这样:

    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);
}

关于c# - 在 C# 中使用 CheckBoxes 的 TreeView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5478984/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com