In C# Winforms using Containers (Panel, GroupBox, Forms or user controls) is very common.
So Finding a control from a container or user control needs a recursive search
, If we have number of child containers inside a parent container.
The below snippet will do the recursive stuff. The argument 'ParentCntl'
is the container or user control name and the 'NameToSearch'
is the name of the control to search.
public Control RecursiveFind(Control ParentCntl, string NameToSearch)
if (ParentCntl.ID == NameToSearch)
foreach (Control ChildCntl in ParentCntl.Controls)
Control ResultCntl = RecursiveFind(ChildCntl , NameToSearch);
if (ResultCntl != null)
return ResultCntl ;