I have a window form that has a couple of combo boxes.
What I need accomplished is to have every combo box that has items to have their selected index set to 0. Rather than do every combo box by name manually, is there a function that returns all the children by type. Or to get all the children and compare their type to that of the combobox element?
@noah, your original code did not work at first, but all I needed was the reminder that the children are called controls (thanks).
With that I made a recursive function that sets all combo boxes not just the direct descendants of the main form:
private void recursiveSetComboBox(Control element)
{
foreach (Control a in element.Controls)
{
if (a.Controls.Count != 0)
recursiveSetComboBox(a);
else if (a.GetType().Name == "ComboBox")
{
ComboBox b = (ComboBox)a;
b.SelectedIndex = 0;
}
}
}
recursiveSetComboBox(this);
This is how I would do it:
If the combo boxes are not directly on the form but inside a container (group box, panel, etc.) then use that instead of myForm.