TreeView 递归选择父节点和子节点

 

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
   
if (e.Action != TreeViewAction.ByMouse)
       
return;
    SetParentNode(e.Node);
    SetChildNode(e.Node);
}

private void SetParentNode(TreeNode node)
{
   
if (node.Level > 0)
    {
        node.Parent.Checked
= true;
        SetParentNode(node.Parent);
    }
}

private void SetChildNode(TreeNode node)
{
   
foreach (TreeNode childNode in node.Nodes)
    {
        childNode.Checked
= node.Checked;
       
if (node.Nodes.Count > 0)
SetChildNode(childNode);
    }
}
posted @ 2012-04-18 22:30  跟着阿笨一起玩.NET  阅读(437)  评论(0编辑  收藏  举报