主题:treeView递归問題
我用下面的代码,想把选中的节点的所以子节点的Checked为true,,不知道为什么错误..大家帮忙看看..
private void Form1_Load(object sender, System.EventArgs e)
{
treeView1.CheckBoxes=true;
TreeNode node;
for(int x=0;x<4;x++)
{
node=treeView1.Nodes.Add(string.Format("節點{0}",x*4));
for(int y=0;y<4;y++)
{
node.Nodes.Add(string.Format("節點{0}",x+y*4));
}
}
}
private void treeView1_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
SetCheck(e.Node,e.Node.Checked);
}
private void SetCheck(TreeNode node,bool check)
{
node.Checked=check;
foreach(TreeNode n in node.Nodes)
{
if(node.Nodes.Count==0)
node.Checked=check;
else
SetCheck(n,check);
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
treeView1.CheckBoxes=true;
TreeNode node;
for(int x=0;x<4;x++)
{
node=treeView1.Nodes.Add(string.Format("節點{0}",x*4));
for(int y=0;y<4;y++)
{
node.Nodes.Add(string.Format("節點{0}",x+y*4));
}
}
}
private void treeView1_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
SetCheck(e.Node,e.Node.Checked);
}
private void SetCheck(TreeNode node,bool check)
{
node.Checked=check;
foreach(TreeNode n in node.Nodes)
{
if(node.Nodes.Count==0)
node.Checked=check;
else
SetCheck(n,check);
}
}