Finding a control recusrsively in C# WinForms.

Posted by Peermohamedmydeen under Windows Forms category on | Points: 40 | Views : 9160
Hello All,

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)
return ParentCntl;

foreach (Control ChildCntl in ParentCntl.Controls)

Control ResultCntl = RecursiveFind(ChildCntl , NameToSearch);

if (ResultCntl != null)

return ResultCntl ;


return null;


PMM :)

Comments or Responses

Login to post response