Winform TreeView 查找下一个节点

Posted 跟着阿笨一起玩NET

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Winform TreeView 查找下一个节点相关的知识,希望对你有一定的参考价值。

转载:http://www.cnblogs.com/Ruiky/archive/2013/02/01/2888674.html

public static class TreeViewHelper
    {
        private static IEnumerable<TreeNode> childNodes(this TreeNode node)
        {
            return node.Nodes.Cast<TreeNode>()
                       .SelectMany(x => x.selfAndChildNodes());
        }
 
        private static IEnumerable<TreeNode> selfAndChildNodes(this TreeNode node)
        {
            return new List<TreeNode>() { node }
                       .Union(node.Nodes.Cast<TreeNode>()
                       .SelectMany(x => x.selfAndChildNodes()));
        }
 
        private static IEnumerable<TreeNode> nextNodes(this TreeView tree, TreeNode node)
        {
            bool after = false;
            if (node.Parent == null)
                return tree.Nodes.Cast<TreeNode>()
                           .Select(x => { bool b = after = after || (node == x); return new { x, b }; })
                           .Where(x => x.b && x.x != node)
                           .SelectMany(x => new List<TreeNode>() { x.x }.Union(x.x.childNodes()));
            else
                return node.Parent.Nodes.Cast<TreeNode>()
                           .Select(x => { bool b = after = after || (node == x); return new { x, b }; })
                           .Where(x => x.b && x.x != node)
                           .SelectMany(x => new List<TreeNode>() { x.x }.Union(x.x.childNodes()))
                           .Union(tree.nextNodes(node.Parent));
        }
 
        public static IEnumerable<TreeNode> NextNodes(this TreeView tree, TreeNode node)
        {
            if (tree == null || node == null)
                return new List<TreeNode>();
            return node.childNodes().Union(tree.nextNodes(node));
        }
 
        public static IEnumerable<TreeNode> NextNodes(this TreeView tree)
        {
            if (tree == null || tree.SelectedNode == null)
                return new List<TreeNode>();
            return tree.NextNodes(tree.SelectedNode);
        }
    }

  

  如何调用:

var tn = _Tv.NextNodes().FirstOrDefault(x => Regex.IsMatch(x.Text, "(?i)" + txtKey.Text.Trim()));

 if (tn == null)                

    MessageBox.Show("查找完毕");            

else                

_Tv.SelectedNode = tn;

 

复制代码
public static IEnumerable<TreeNode> TreeWhere(this TreeNodeCollection tds, Func<TreeNode, bool> Fun)
        {
            foreach (TreeNode item in tds)
            {
                if (Fun(item))
                    yield return item;

                var list = item.Nodes.TreeWhere(Fun);
                foreach (var item1 in list)
                    yield return item1;
            }
        }
复制代码

如何调用:

 var tn = treeView1.Nodes.TreeWhere(x => Regex.IsMatch(x.Text, @"(?i)" + txtKey.Text.Trim()));

 
分类: C#,WinForm

以上是关于Winform TreeView 查找下一个节点的主要内容,如果未能解决你的问题,请参考以下文章

winform中,TreeView如何只展开一级节点

C# winform treeview 节点展开状态的图标设置

C#如何在treeview控件winform的所有父子节点中搜索文本

winform 中Treeview控件怎样选中父节点时子节点全部展开,选中其他父节点时原先展开的关闭

Winform 中使用treeview控件,treeview控件会默认选择最后一个有子节点的节点,怎么解决

WinForm控件TreeView 只部分节点显示 CheckBox