private void treeView1_AfterCheck(object sender, TreeVIEwEventArgs e)
{
oMM.ToggleAllChildNodes(e.Node,e.Node.Checked);
treeView1.AfterCheck-=new TreeViewEventHandler(treeVIEw1_AfterCheck);
oMM.ToggleAllParentNodes(e.Node,e.Node.Checked);
treeView1.AfterCheck+=new TreeViewEventHandler(treeVIEw1_AfterCheck);
//遍歷父結點 時取消事件以免無限循環
}
public void ToggleAllChildNodes(TreeNode CurrentNode ,bool NodeChecked)
{
foreach(TreeNode node in CurrentNode.Nodes)
{
node.Checked=NodeChecked;
if(node.Nodes.Count>0)
this.ToggleAllChildNodes(node,NodeChecked);
}
}
public void ToggleAllParentNodes(TreeNode CurrentNode ,bool NodeChecked)
{
if(NodeChecked)
{
CurrentNode.Checked=NodeChecked;
if(CurrentNode.Parent!=null)
{
this.ToggleAllParentNodes(CurrentNode.Parent,NodeChecked);
}
}
}