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()));
Winform TreeView 查找下一个节点的更多相关文章
- C# Winform 按回车键查找下一个可设置焦点的组件
private void frmLogin_KeyPress(object sender, KeyPressEventArgs e) { //按回车键查找下一个可设置焦点的组件. if (e.KeyC ...
- javascript 获取下一个节点
下一个节点: nextElementSibling 上一个节点 previousElementSibling <div> <select onchange="alert(t ...
- 剑指offer-二叉树的下一个节点
题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 解题思路 分情况考虑如下: 若该节点为空,则直 ...
- 剑指offer(57)二叉树的下一个节点
题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 题目分析 这题一定要画图,因为只有画图我们才能分清楚下 ...
- 【剑指offer】08二叉树的下一个节点,C++实现
原创博文,转载请注明出处! # 题目 父节点指向子节点的指针用实线表示,从子节点指向父节点的指针用虚线表示. # 思路 如果节点有右子节点,则右子节点的最左节点是该节点的下一个节点.例如,寻找b的下一 ...
- javascript 获取dom书的下一个节点。
利用javascript 写一个在页面点击加减按钮实现数字的累加.. 简略的html大概如此.看得懂就好不要在意这些细节啊 <input type="button" valu ...
- 剑指Offer的学习笔记(C#篇)-- 二叉树的下一个节点(好理解版本)
题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 一 . 理解题意 该题目我们可以借鉴一个非常影响不好的 ...
- 剑指Offer(书):二叉树的下一个节点
题目:给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 分析:若一个节点有右子树,那么他的下一个节点就是他右子树中 ...
- 剑指offer第二版-8.二叉树的下一个节点
描述:给定一棵二叉树和其中的一个节点,找出中序遍历序列的下一个节点.树中应定义指向左节点.右节点.父节点的三个变量. 思路: 1.如果输入的当前节点有右孩子,则它的下一个节点即为该右孩子为根节点的子树 ...
随机推荐
- thymeleaf 中文乱码问题
使用thymeleaf后,即使使用org.springframework.web.filter.CharacterEncodingFilter也不能解决中文乱码问题了, 后来发现在org.thymel ...
- JDBC连接数据库(SQLServer和MySQL)配置总结
[JDBC连接SQL Server] [准备工作] 1.下载Eclipse-JavaEE: 2.下载Tomcat8.0: 3.下载Microsoft JDBCDriver 4.1 for SQL Se ...
- html添加网络音乐
IE浏览器,其他的不一定适应 <embed src="http://www.kmfhsj.com/fish-photo/music/xiaochenggushi.mp3" a ...
- FreeBSD_11-系统管理——{Part_0-基础}
Tips: sysctl -d kern.maxvnodes #查看系统控制选项的含义 true > file #清空文件内容 alias ls 'ls -I(大写i)' #取消 root 的 ...
- Action类为何要继承ActionSupport
Action类为何要继承ActionSupport 理论上Struts 2.0的Action无须实现任何接口或继承任何类型,但是,我们为了方便实现Action,大多数情况下都会继承com.open ...
- 【堆】【kd-tree】bzoj2626 JZPFAR
用堆记录答案.看看当前点是否比堆顶更优. #include<cstdio> #include<queue> #include<cstring> #include&l ...
- [Leetcode][JAVA] Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- Rhythmbox中文乱码
今天在网络上找到了一个比较好的解决Rhythmbox中文乱码的问题的方法 进入你的音乐文件夹执行如下代码: mid3iconv -e GBK *.mp3 如果没有提示多试几次, 有可能系统会提示: p ...
- Spring MVC 3 深入总结
一.前言: 大家好,Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了.Spring3 MVC结构简单,应了那句话简单就是美 ...
- PropertyChangedCallback 只触发了一次?
在自定义的用户控件中,添加一个依赖属性,如下: public static readonly DependencyProperty ItemsSourceProperty = DependencyPr ...