I have written a few extensions to Control.ControlCollection class for Winforms.
These extensions are lambda expression wrappers and other useful API.
You can also write LINQ queries against the ControlCollection using an extension.
Extensions :
C#
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespaceSystem.Windows.Forms
{
publicstaticclassControlCollectionExtensions
{
publicstaticvoidForEach(this Control.ControlCollection ControlCollection, Action<Control> Action)
{
for (int i = 0; i < ControlCollection.Count; i++)
{
Action.Invoke(ControlCollection[i]);
}
}
publicstaticvoidFor(this Control.ControlCollection ControlCollection, int Start, int End, int Step, Action<Control> Action)
{
for (int i = Start; i < End; i++)
{
Action.Invoke(ControlCollection[i]);
}
}
publicstaticvoidFor(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]);
}
}
publicstatic 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;
}
publicstatic 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;
}
publicstatic 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;
}
}
}
Sample Usage :
C#
//Find control of type RichTextBox
List<Control> lstControls = this.Controls.FindControlsByType(typeof(RichTextBox), true);
//Where control Name is rtbScratchPad
List<Control> lstControls1 = this.Controls.Where(ctrl => ctrl.Name == "rtbScratchPad", true);
//For each control in the collectionthis.Controls.ForEach(ctrl => MessageBox.Show(ctrl.Name));
//For every second control in the collectionthis.Controls.For(0, this.Controls.Count - 1, 2, ctrl => MessageBox.Show(ctrl.Name));
//LINQ query on ControlCollection using ToList() extension
Control c = (from ctrl inthis.Controls.ToList(true)
where ctrl.Name == "rtbScratchPad"select ctrl).Single();
0 comments
Comments
Join the conversation! Your thoughts help the community grow.
Join to leave a comment
Members earn 10 SHARP for a comment, 10 SHARP for a reaction, and 250 SHARP for every blog they publish.
Join the conversation! Your thoughts help the community grow.