Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

foreach Control ctrl in SomePanel.Controls does not get all controls

I have a panel with a bunch of labeles and textboxes inside of it.

The code:

foreach (Control ctrl in this.pnlSolutions.Controls)

Seems to only be finding html table inside the panel and 2 liternals. But it does not get the textboxes that are in the html table. Is there a simple way to get all the controls inside of a panel regardless of the nesting?

thanks!

like image 635
aron Avatar asked Dec 04 '22 12:12

aron


1 Answers

Here's a lazy solution:

public IEnumerable<Control> GetAllControls(Control root) {
  foreach (Control control in root.Controls) {
    foreach (Control child in GetAllControls(control)) {
      yield return child;
    }
  }
  yield return root;
}

Remember also that some controls keep an internal collection of items (like the ToolStrip) and this will not enumerate those.

like image 69
Ron Warholic Avatar answered Jan 05 '23 00:01

Ron Warholic