日期:2014-05-19  浏览次数:20722 次

怎么样得到treeview的全部所选节点呢?
treeview开启了多选框,怎么得到所有所选节点上文字呢?

------解决方案--------------------
private void PrintRecursive(TreeNode treeNode)
{
// Print the node.
System.Diagnostics.Debug.WriteLine(treeNode.Text);
MessageBox.Show(treeNode.Text);
// Print each node recursively.
foreach (TreeNode tn in treeNode.Nodes)
{
if (tn.Checked==true)
{
ary.add(tn.Tag);//加到集合中
}

if(tn.Nodes.Count > 0)
{
PrintRecursive(tn);
}
}
}

------解决方案--------------------
dinglj1760(阿拉丁) 的方法不能正确显示第三级节点。下面是我修改后的代码,注释部分是原来代码

private void PrintRecursive(TreeNode treeNode)
{
// Print the node.
//System.Diagnostics.Debug.WriteLine(treeNode.Text);

if (treeNode.Checked == true)
{
MessageBox.Show(treeNode.Name+ "- "+treeNode.Text);
}
// Print each node recursively.
foreach (TreeNode tn in treeNode.Nodes)
{
//if (tn.Checked == true)
//{
//MessageBox.Show(treeNode.Text);
PrintRecursive(tn);
//}

}

}

// Call the procedure using the TreeView.
private void CallRecursive(TreeView treeView)
{
// Print each node recursively.
TreeNodeCollection nodes = treeView.Nodes;
//MessageBox.Show(nodes.Count.ToString());
foreach (TreeNode n in nodes)
{
PrintRecursive(n);
}
}