是否可以通过提供节点数

时间:2016-01-20 11:59:24

标签: c# .net winforms treeview

我知道使用tagtext可以搜索节点并找到它。

treeview1.Find(string name, bool searchAllChildren)

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

让我们在XML中有一个如下图所示的结构。我的目标是以下列方式生成树视图。

首先,将所有节点和子节点复制到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

是否有这样的方式,或者只有一个人可以通过这样做来访问节点,

1 个答案:

答案 0 :(得分:3)

首先,我不会重新发明轮子,而是从我对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或其他一些标准搜索)等。

更新

现在奇怪的要求,但这里是&#34; raw&#34; 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);