Wednesday, May 26, 2010

Find controls of specific type

Find all controls of specific type by providing main control and complete type name.

public static void FindAllControlsByType(Control MainControl, string TypeName, System.Collections.ArrayList FoundControls)
{
// make sure it is ok to procees
if (MainControl == null || FoundControls == null)
return;

// iterate and search for required controls
foreach (Control child in MainControl.Controls)
{
if (child.GetType().ToString().Equals(TypeName))
FoundControls.Add(child);

// it may have nested controls
if (child.Controls.Count > 0)
FindAllControlsByType(child, TypeName, FoundControls);
}
}

No comments:

Post a Comment