日期:2014-05-17 浏览次数:20909 次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
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(