C# WinForm 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 @ 2011-08-02 17:06  一浩瀚星空一  阅读(2879)  评论(0编辑  收藏  举报