Reprint: 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 to 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 (t REE = = 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); } }
How to Invoke:
var tn = _tv.nextnodes (). FirstOrDefault (x = Regex.IsMatch (X.text, "(? i)" + TxtKey.Text.Trim ()));
if (tn = = NULL)
MessageBox.Show ("Find complete");
Else
_tv.selectednode = TN;
public static ienumerable<treenode> Treewhere (this treenodecollection TDs, Func<treenode, bool> fun) { foreach (TreeNode item in TDS) { if (item) yield return item; var list = Item. Nodes.treewhere (fun); foreach (var item1 in list) yield return item1; } }
How to Invoke:
var tn = treeView1.Nodes.TreeWhere (x = Regex.IsMatch (X.text, @ "(? i)" + TxtKey.Text.Trim ()));
Category: C#,winform
Winform TreeView to find the next node