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.如果输入的当前节点有右孩子,则它的下一个节点即为该右孩子为根节点的子树 ...
随机推荐
- iOS(OC)中的冒泡排序
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"12",@"84", @"35& ...
- dropbear
生成ssh连接所需要的公钥,如下: /usr/bin/dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key (dss加密,长度默认 ...
- 无向图最小生成树(prim算法)
普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点,且其所有边的权值之和亦为最小.该算法于1930年由捷 ...
- mysql基本命令(转)
连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root -p,回车后提示你输密码.注意用户名前可以有空格也可以没有空格,但是密码前必须没有空 ...
- MQ安装配置
(1)执行接受许可脚本: ./mqlicense.sh –accept (2) 安装 WebSphere MQ for Linux 服务器: [root@localhost mq]# rpm ...
- 斯坦福第十课:应用机器学习的建议(Advice for Applying Machine Learning)
10.1 决定下一步做什么 10.2 评估一个假设 10.3 模型选择和交叉验证集 10.4 诊断偏差和方差 10.5 归一化和偏差/方差 10.6 学习曲线 10.7 决定下一步做什么 ...
- Java里的if else语句例子
import java.util.Scanner;public class if_else2{ public static void main(String[] args) { Scanner s=n ...
- office2003-2007 绿色版 出错 文件丢失(未解决)
- 这个版本是我大学时候(2012)年一直用到现在的版本:目录结构如下: 原来一直在32位系统中使用,没有出错过; - 刚装的两台电脑系统分别为 Win7Pro 和 Win10Pro ,都是64位的: ...
- [数据结构] N皇后问题
代码: #include <iostream> #include <string.h> #include <algorithm> using namespace s ...
- 11-30 k线图demo中学到的零散知识
1. 使用NSObject类的方法performSelectorInBackground:withObject:来创建一个线程. 具体的代码: [Object performSelectorInBac ...