private void tvSysFunctionMenu_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
...{
if (e.Node != tvSysFunctionMenu.Nodes[0])
...{
SetChildNodeChecked(e.Node);
SetParentNodeChecked(e.Node);
}
else
...{
SetChildNodeChecked(e.Node);
}
}
//根據父節點狀態設置子節點的狀態
private void SetChildNodeChecked(TreeNode parentNode)
...{
foreach (TreeNode node in parentNode.Nodes)
...{
node.Checked = parentNode.Checked;
if (node.Nodes.Count > 0)
...{
SetChildNodeChecked(node);
}
}
}
//根據子節點狀態設置父節點的狀態
private void SetParentNodeChecked(TreeNode childNode)
...{
TreeNode parentNode = childNode.Parent;
if (!parentNode.Checked && childNode.Checked) //當子結點選中,父結點沒選中時,則:選中父結點
...{
parentNode.Checked = true;
}
else if (parentNode.Checked && !childNode.Checked) //取消選中其下全部子結點時,其父結點也取消
...{
int checks = 0; //統計子結點的個數(為0時表示取消了其下全部子結點)
foreach (TreeNode node in parentNode.Nodes)
...{
if (node.Checked)
...{
checks++;
break;
}
}
if (checks == 0) //子結點都取消選中時,其父結點也取消
...{
parentNode.Checked = false;
if (!parentNode.Index.Equals(0))
...{
SetParentNodeChecked(parentNode);
}
}
}
}
//節點選中狀態更改
protected void TreeNodeCheckStatusChanged(object sender, TreeNodeMouseClickEventArgs e)
...{
SetChildNodeChecked(e.Node);
// 判斷是否是根節點
if (!e.Node.Index.Equals(0))
...{
SetParentNodeChecked(e.Node);
}
}