using System; using System.Collections.Generic; using System.Windows.Forms;
namespace System.Windows.Forms { public static class ControlCollectionExtensions { public static void ForEach(this Control.ControlCollection ControlCollection, Action<Control> Action) { for (int i = 0; i < ControlCollection.Count; i++) { Action.Invoke(ControlCollection[i]); } } public static void For(this Control.ControlCollection ControlCollection, int Start, int End, int Step, Action<Control> Action) { for (int i = Start; i < End; i++) { Action.Invoke(ControlCollection[i]); } } public static void For(this Control.ControlCollection ControlCollection, int Start, Func<int, bool> End, int Step, Action<Control> Action) { for (int i = Start; End.Invoke(i); i++) { Action.Invoke(ControlCollection[i]); } } public static List<Control> Where(this Control.ControlCollection ControlCollection, Func<Control, bool> Condition, bool SearchAllChildren) { List<Control> lstControls = new List<Control>(); ControlCollection.ForEach(ctrl => { if (SearchAllChildren) { lstControls.AddRange(ctrl.Controls.Where(Condition, true)); } if (Condition.Invoke(ctrl)) lstControls.Add(ctrl); }); return lstControls; } public static List<Control> FindControlsByType(this Control.ControlCollection ControlCollection, Type Type, bool SearchAllChildren) { List<Control> lstControls = new List<Control>(); ControlCollection.ForEach(ctrl => { if (SearchAllChildren) { lstControls.AddRange(ctrl.Controls.FindControlsByType(Type, true)); } if (ctrl.GetType() == Type) lstControls.Add(ctrl); });
return lstControls; } public static List<Control> ToList(this Control.ControlCollection ControlCollection, bool IncludeChildren) { List<Control> lstControls = new List<Control>(); ControlCollection.ForEach(ctrl => { if (IncludeChildren) lstControls.AddRange(ctrl.Controls.ToList(true)); lstControls.Add(ctrl); }); return lstControls; } } }
|