C# 获取TreeView上的选定节点索引

C# 获取TreeView上的选定节点索引,c#,winforms,C#,Winforms,我想获得所选节点的确切索引号 我使用下面的代码,但是,它没有显示确切的索引 treeView1.SelectedNode.Index a b c d e f--> If i click on 'f' i want to get '6' value 如何在treeview上获取所选节点的索引?如您所知,treeview控件是树节点的分层集合,其中每个树节点可能都有一个子树节点列表。树节点的索引属性是它在兄弟树节点集合中的(基于零的)索引。因此,在上面的示例中,Tre

我想获得所选节点的确切索引号

我使用下面的代码,但是,它没有显示确切的索引

treeView1.SelectedNode.Index


a
 b
  c 
 d
  e
   f--> If i click on 'f' i want to get '6' value

如何在treeview上获取所选节点的索引?

如您所知,treeview控件是树节点的分层集合,其中每个树节点可能都有一个子树节点列表。树节点的索引属性是它在兄弟树节点集合中的(基于零的)索引。因此,在上面的示例中,TreeNode“f”如果选中,将返回索引值0,因为它是该级别的第一个TreeNode。如果我用每个节点对应的索引绘制示例树,您将看到如下内容:

a 0
 b 0
  c 0
 d 1  <-- because 'd' is the second node at this level. 'b' is index 0
  e 0
   f 0
a 0
 b 1
  c 2
 d 3
  e 4
   f 5
private int GetIndex(TreeNode node)
{
    // Always make a way to exit the recursion.
    if (node.Parent == null)
        return node.Index;

    return node.Index + GetIndex(node.Parent);
}
现在,我知道您说过希望它返回6,而不是5,但在大多数现代语言中,索引从0开始,而不是1

好的,如果我们从一个简单的递归函数开始爬树,我们会得到如下结果:

a 0
 b 0
  c 0
 d 1  <-- because 'd' is the second node at this level. 'b' is index 0
  e 0
   f 0
a 0
 b 1
  c 2
 d 3
  e 4
   f 5
private int GetIndex(TreeNode node)
{
    // Always make a way to exit the recursion.
    if (node.Parent == null)
        return node.Index;

    return node.Index + GetIndex(node.Parent);
}
现在,上面的代码将在树上递归,但它不会给我们正确的答案。为什么?因为如果我们正在评估的树节点在索引中有较高的兄弟节点(在您的示例中,节点“b”到节点“d”),那么我们将缺少子节点。因为子节点可以有子节点(on和on…),所以我们有另一个递归函数

递归第二轮:

private int GetIndex(TreeNode node)
{
    int returnValue = 0;

    // Always make a way to exit the recursion.
    if (node.Index == 0 && node.Parent == null)
        return returnValue;

    // Now, count every node.
    returnValue = 1;

    // If I have siblings higher in the index, then count them and their decendants.
    if (node.Index > 0)
    {
        TreeNode previousSibling = node.PrevNode;
        while (previousSibling != null)
        {
            returnValue += GetDecendantCount(previousSibling);
            previousSibling = previousSibling.PrevNode;
        }
    }

    if (node.Parent == null)
        return returnValue;
    else
        return returnValue + GetIndex(node.Parent);
}

public int GetDecendantCount(TreeNode node)
{
    int returnValue = 0;

    // If the node is not the root node, then we want to count it.
    if (node.Index != 0 || node.Parent != null)
        returnValue = 1;

    // Always make a way to exit a recursive function.
    if (node.Nodes.Count == 0)
        return returnValue;

    foreach (TreeNode childNode in node.Nodes)
    {
        returnValue += GetDecendantCount(childNode);
    }
    return returnValue;
}

这应该满足您的要求。

您可能知道,TreeView控件是树节点的分层集合,其中每个树节点可能有一个子树节点列表。树节点的索引属性是它在兄弟树节点集合中的(基于零的)索引。因此,在上面的示例中,TreeNode“f”如果选中,将返回索引值0,因为它是该级别的第一个TreeNode。如果我用每个节点对应的索引绘制示例树,您将看到如下内容:

a 0
 b 0
  c 0
 d 1  <-- because 'd' is the second node at this level. 'b' is index 0
  e 0
   f 0
a 0
 b 1
  c 2
 d 3
  e 4
   f 5
private int GetIndex(TreeNode node)
{
    // Always make a way to exit the recursion.
    if (node.Parent == null)
        return node.Index;

    return node.Index + GetIndex(node.Parent);
}
现在,我知道您说过希望它返回6,而不是5,但在大多数现代语言中,索引从0开始,而不是1

好的,如果我们从一个简单的递归函数开始爬树,我们会得到如下结果:

a 0
 b 0
  c 0
 d 1  <-- because 'd' is the second node at this level. 'b' is index 0
  e 0
   f 0
a 0
 b 1
  c 2
 d 3
  e 4
   f 5
private int GetIndex(TreeNode node)
{
    // Always make a way to exit the recursion.
    if (node.Parent == null)
        return node.Index;

    return node.Index + GetIndex(node.Parent);
}
现在,上面的代码将在树上递归,但它不会给我们正确的答案。为什么?因为如果我们正在评估的树节点在索引中有较高的兄弟节点(在您的示例中,节点“b”到节点“d”),那么我们将缺少子节点。因为子节点可以有子节点(on和on…),所以我们有另一个递归函数

递归第二轮:

private int GetIndex(TreeNode node)
{
    int returnValue = 0;

    // Always make a way to exit the recursion.
    if (node.Index == 0 && node.Parent == null)
        return returnValue;

    // Now, count every node.
    returnValue = 1;

    // If I have siblings higher in the index, then count them and their decendants.
    if (node.Index > 0)
    {
        TreeNode previousSibling = node.PrevNode;
        while (previousSibling != null)
        {
            returnValue += GetDecendantCount(previousSibling);
            previousSibling = previousSibling.PrevNode;
        }
    }

    if (node.Parent == null)
        return returnValue;
    else
        return returnValue + GetIndex(node.Parent);
}

public int GetDecendantCount(TreeNode node)
{
    int returnValue = 0;

    // If the node is not the root node, then we want to count it.
    if (node.Index != 0 || node.Parent != null)
        returnValue = 1;

    // Always make a way to exit a recursive function.
    if (node.Nodes.Count == 0)
        return returnValue;

    foreach (TreeNode childNode in node.Nodes)
    {
        returnValue += GetDecendantCount(childNode);
    }
    return returnValue;
}

这应该可以满足您的要求。

层次结构上的索引是什么?@AdrianoRepetti没有回答您的问题。treeView1.SelectedNode.index向您显示了什么以及您希望看到什么?@C.Dhruv已编辑。
treeView1.SelectedNode.index
现在得到了什么值?层次结构上的索引是什么?@AdrianoRepetti没有收到您的问题。TreeView w1.SelectedNode.Index向您显示了什么以及您希望看到什么?@C.Dhruv已编辑。您现在为
TreeView.SelectedNode.Index
获得了什么值?解释得很好!解释得很好!