實(shí)現(xiàn)C# TreeView的AfterCheck事件中的Checked,主要有以下情況:
1.父節(jié)點(diǎn)勾選則子節(jié)點(diǎn)全部勾選
2.父節(jié)點(diǎn)不勾選則子節(jié)點(diǎn)全部不勾選
3.子節(jié)點(diǎn)全部不勾選則父節(jié)點(diǎn)不勾選
4.子節(jié)點(diǎn)部分勾選則父節(jié)點(diǎn)不勾選
5.子節(jié)點(diǎn)全部勾選則父節(jié)點(diǎn)被勾選
以下是遞歸實(shí)現(xiàn)
private void SetNodeCheckStatus(TreeNode tn, bool chk)
{
if (tn == null)
return;
// Set child nodes check status
foreach( TreeNode tnChild in tn.Nodes)
{
tnChild.Checked = chk;
SetNodeCheckStatus(tnChild, chk);
}
// Set parent check status
int nNodeCount = 0;
TreeNode tnParent = tn;
while(tnParent.Parent != null)
{
nNodeCount = 0;
tnParent = tnParent.Parent;
foreach(TreeNode tnTmp in tnParent.Nodes)
{
if (tnTmp.Checked == chk)
{
nNodeCount++;
}
}
if (nNodeCount == tnParent.Nodes.Count)
tnParent.Checked = chk;
else
tnParent.Checked = false;
}
}
網(wǎng)頁題目:在TreeView中使用CheckBox
本文網(wǎng)址:
http://weahome.cn/article/jhippo.html