Form1.ActiveForm.Controls is a collection of all controls on the active form. These could be any control type - TextBox, Label, Panel, not just ComboBox.
Try this.
foreach (Control a in Form1.ActiveForm.Controls) //<-- loop object is Control type.
{
if (a is ComboBox) //<-- test if loop object is a ComboBox
{
MessageBox.Show("hello"); // do something
}
}
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
Your also need brackets around the cast item.
((ComboBox)a).Items.Add();
Or use another variable
foreach (Control a in Form1.ActiveForm.Controls)
{
ComboBox combo = a as ComboBox; //<-- converts a to ComboBox type or null
if (combo !=null) // if not null then 'a' was a ComboBox
{
combo.Items.Add("a");
combo.Items.Add("b");
}
}
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
If solved, then please mark the thread solved.
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187