我有递归程序来添加节点t树视图,但我想采用自下而上的方法来做到这一点。
到目前为止我已经这样做了,但它不起作用
private void TreeviewCountCheckedNodes(TreeNodeCollection treeNodeCollection)
{
TreeNode node = treeNodeCollection[0];
int countchecked = 0;
if (node != null)
{
foreach (TreeNode childnode in node.Nodes)
{
if (childnode.Nodes.Count == 0 && childnode.Checked)
{
countchecked++;
}
else if (childnode.Nodes.Count > 0)
{
TreeviewCountCheckedNodes(childnode.Nodes);
}
}
}
}
private int TreeviewCountCheckedNodes(TreeNodeCollection treeNodeCollection)
{
int countchecked = 0;
if (treeNodeCollection != null)
{
foreach (TreeNode node in treeNodeCollection)
{
if (node.Nodes.Count == 0 && node.Checked)
{
countchecked++;
}
else if (node.Nodes.Count > 0)
{
countchecked += TreeviewCountCheckedNodes(node.Nodes);
}
}
}
return countchecked;
}
致电:
int coount = TreeviewCountCheckedNodes(treeView.Nodes);
我在这里发布这段代码,认为它可能对某人有帮助。
代码检索树视图中所有检查节点的计数。
int checkedNodesCount = 0;
private int GetCheckedNodesCount(TreeNodeCollection treeNodeCollection)
{
TreeNode node = treeNodeCollection[0];
if (node != null)
{
if (node.Nodes.Count > 0)
{
foreach (TreeNode childnode in node.Nodes)
{
if (childnode.Nodes.Count == 0 && childnode.Checked)
{
checkedNodesCount++;
}
else if (childnode.Nodes.Count > 0)
{
checkedNodesCount += GetCheckedNodesCount(childnode.Nodes);
}
}
}
else
{
if (node.Checked)
{
checkedNodesCount++;
}
}
}
return checkedNodesCount;
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Node : " + GetChildNodesCount(treeView1.Nodes) ;
}
private int GetChildNodesCount(TreeNodeCollection nodes)
{
int nodeC = nodes.Count ;
foreach(TreeNode node in nodes)
{
nodeC += GetChildNodesCount(node.Nodes) ;
}
return nodeC ;
}