是否有任何方法可以通过提供节点数来访问树视图节点

Is ther any way to get access to a treeview node by giving number of node

我知道通过使用 tagtext 可以搜索并找到一个节点。

treeview1.Find(string name, bool searchAllChildren)

但在我的代码中,我有一个 for 整数循环来进行一些计算,然后我想将树视图的 i-th 节点复制到一个新节点。例如我想给 i = 3 它应该给我 node5 和所有 children。

让我们在XML中有一个像下图这样的结构。我的目标是通过以下方式生成树视图。

首先,复制所有节点和children直到nodeIV,然后复制节点node1, .... node7并在一定条件下保存它们,否则创建空节点。

-root
    -nodeI
    -nodeII
    -nodeIII
    -nodeIV
       -node1
          -children1
          -children2
          -children3
       -node2
       -node3
          -childrenA
          -childrenB
          -childrenC
       -node4
       -node5
       -node6
          -children
       -node7
          -childrenG
          -childrenR1
          -childrenR2
          -childrenR3
          -children

有没有这样的方法或者只有一个人可以访问节点,

首先,我不会重新发明轮子,而是从我对 How to flatten tree via LINQ? 的回答中提取辅助函数,这对任何树状结构都非常有用:

public static class TreeUtils
{
    public static IEnumerable<T> Expand<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> elementSelector)
    {
        var stack = new Stack<IEnumerator<T>>();
        var e = source.GetEnumerator();
        try
        {
            while (true)
            {
                while (e.MoveNext())
                {
                    var item = e.Current;
                    yield return item;
                    var elements = elementSelector(item);
                    if (elements == null) continue;
                    stack.Push(e);
                    e = elements.GetEnumerator();
                }
                if (stack.Count == 0) break;
                e.Dispose();
                e = stack.Pop();
            }
        }
        finally
        {
            e.Dispose();
            while (stack.Count != 0) stack.Pop().Dispose();
        }
    }
}

然后我会创建几个 TreeView 特定的助手:

public static class TreeViewUtils
{
    public static IEnumerable<TreeNode> AsEnumerable(this TreeNodeCollection source)
    {
        return source.Cast<TreeNode>();
    }

    public static IEnumerable<TreeNode> All(this TreeNodeCollection source)
    {
        return source.AsEnumerable().Expand(node => node.Nodes.Count > 0 ? node.Nodes.AsEnumerable() : null);
    }

    public static TreeNode Find(this TreeNodeCollection source, int index)
    {
        return source.All().Skip(index).FirstOrDefault();
    }
}

现在要获取树视图的 i-th 节点,您可以简单地使用

var node = treeview1.Nodes.Find(i);

此外,您可以像这样获取任何节点的 i-th 子节点

var childNode = node.Nodes.Find(i);

总而言之,我可以很容易地提供一个递归函数,它只用更少的代码就可以解决特定的问题,但是这个小实用函数为您提供了更多 - foreach 支持,子树 DFT 的所有节点遍历、LINQ 查询(如按 Tag 或其他一些条件搜索)等

更新

如今的需求很奇怪,但这里是 "raw" C# 函数(没有 LINQ、没有扩展方法、没有迭代器方法),它可以满足您的需求

public static class TreeViewUtils
{
    public static TreeNode FindNode(TreeNodeCollection nodes, int index)
    {
        int offset = -1;
        return FindNode(nodes, ref offset, index);
    }
    private static TreeNode FindNode(TreeNodeCollection nodes, ref int offset, int index)
    {
        for (int i = 0; i < nodes.Count; i++)
        {
            var node = nodes[i];
            if (++offset == index || (node = FindNode(node.Nodes, ref offset, index)) != null)
                return node;
        }
        return null;
    }
}

以及各自的用法

var node = TreeViewUtils.FindNode(treeview1.Nodes, i);

var childNode = TreeViewUtils.FindNode(node.Nodes, i);