//******************************************************************************
//
// 文 件 名: TreeUtil.cs
//
// 描 述: 对树进行操作的工具
//
// 作 者: 陈飞
//
// 地 点: 恒亿电子
//
// 创建时间: 2012-04-01
//******************************************************************************
using System.Windows.Forms;
namespace AlarmClient.Common
{
/// <summary>
/// 对树进行操作的回调
/// </summary>
/// <param name="node">要操作的节点</param>
/// <returns>如果确认已处理,返回 true,否则返回 false.</returns>
public delegate bool TreeNodeCallback(TreeNode node);
/// <summary>
/// 对控件进行操作的回调
/// </summary>
/// <param name="control">要操作的控件</param>
/// <returns>如果确认已处理,返回 true,否则返回 false.</returns>
public delegate bool ControlCallback(Control control);
/// <summary>
/// 对树进行操作的工具
/// </summary>
public static class TreeUtil
{
/// <summary>
/// 递归对节点进行操作,子节点优先.
/// </summary>
/// <param name="nodes">要操作的节点的顶层集合.</param>
/// <param name="tnc">操作回调.</param>
/// <returns>受影响节点的个数</returns>
public static int EachChildFirst(this TreeNodeCollection nodes, TreeNodeCallback tnc)
{
if (tnc == null) return 0;
if (nodes == null || nodes.Count < 1) return 0;
int cnt = 0;
foreach (TreeNode node in nodes)
{
cnt += EachChildFirst(node.Nodes, tnc);
if (tnc(node)) ++cnt;
}
return cnt;
}
/// <summary>
/// 递归对节点进行操作,父节点优先.
/// </summary>
/// <param name="nodes">要操作的节点的顶层集合.</param>
/// <param name="tnc">操作回调.</param>
/// <returns>受影响节点的个数</returns>
public static int EachParentFirst(this TreeNodeCollection nodes, TreeNodeCallback tnc)
{
if (tnc == null) return 0;
if (nodes == null || nodes.Count < 1) return 0;
int cnt = 0;
foreach (TreeNode node in nodes)
{
if (tnc(node)) ++cnt;
cnt += EachParentFirst(node.Nodes, tnc);
}
return cnt;
}
/// <summary>
/// 递归对控件进行操作,子控件优先.
/// </summary>
/// <param name="controls">要操作的控件的顶层集合.</param>
/// <param name="cc">操作回调.</param>
/// <returns>受影响控件的个数</returns>
public static int EachChildFirst(this Control.ControlCollection controls, ControlCallback cc)
{
if (cc == null) return 0;
if (controls == null || controls.Count < 1) return 0;
int cnt = 0;
foreach (Control control in controls)
{
cnt += EachChildFirst(control.Controls, cc);
if (cc(control)) ++cnt;
}
return cnt;
}
/// <summary>
/// 递归对控件进行操作,父控件优先.
/// </summary>
/// <param name="controls">要操作的控件的顶层集合.</param>
/// <param name="cc">操作回调.</param>
/// <returns>受影响控件的个数</returns>
public static int EachParentFirst(this Control.ControlCollection controls, ControlCallback cc)
{
if (cc == null) return 0;
if (controls == null || controls.Count < 1) return 0;
int cnt = 0;
foreach (Control control in controls)
{
if (cc(control)) ++cnt;
cnt += EachParentFirst(control.Controls, cc);
}
return cnt;
}
}
}