How to find control by name

  • Thread starter Thread starter Gregory Khrapunovich
  • Start date Start date
G

Gregory Khrapunovich

I have a string representing the name of the control. Is there a method that
would take the string and return the pointer to the control or I need to
search Controls collection manually? (I work with Windows Forms)
Gregory Khrapunovich
 
Gregory said:
I have a string representing the name of the control. Is there a method
that
would take the string and return the pointer to the control or I need to
search Controls collection manually? (I work with Windows Forms)

I don't think there's a method anywhere that does this. You could
implement it yourself, like this:

Control FindControl(string name, Control searchControl) {
if (searchControl.Name == name)
return searchControl;

foreach (Control childControl in searchRoot.Controls) {
Control foundControl = FindControl(name, childControl);
if (foundControl != null)
return foundControl;
}

return null;
}


Oliver Sturm
 
Back
Top