CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Get all controls of a type from a form / container

172 Views
Copy Code Show/Hide Line Numbers
// 3.5:
var radioGroups = this.Controls.OfType<RadioGroup>().ToList();
 
// old way using recursion:
var radioGroups = GetAllRadioGroups(this.Controls);
 
/// <summary>
/// Return a list of radiogroup controls within the current controls collection, recursively
/// </summary>
/// <param name="controls"></param>
/// <returns></returns>
public static List<RadioGroup> GetAllRadioGroups(this Control.ControlCollection controls)
{
    List<RadioGroup> list = new List<RadioGroup>();
 
    foreach (Control control in controls)
    {
        if (control is RadioGroup)
        {
            list.Add(control as RadioGroup);
        }
        else if (control.HasChildren)
        {
            list.AddRange(control.Controls.GetAllRadioGroups());
        }
    }
 
    return list;
}
 
// Using the list
radioGroups.ForEach(control => control.ItemNotInList += new UltraComboEditor.ItemNotInListEventHandler(InfragisticsHelper.UltraComboEditor_ValidateItemInList));
by Cat
  July 06, 2010 @ 4:33am
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate