You can declare a List to store the value of ComboBox. Example:
public partial class Form1 : Form
{
List<combobox> lst = new List<combobox>();
public Form1()
{
InitializeComponent();
}
}
By the time you create ComboBox dynamically, you add it into the List:
void DynamicAddComboBox()
{
for (int i = 0; i < 3; i++)
{
ComboBox c = new ComboBox();
panel1.Controls.Add(c);
lst.Add(c);
}
}
When you want to get the values of the combobox, just loop through the list. Example:
void GetComboBoxValues()
{
StringBuilder sb = new StringBuilder();
foreach (ComboBox c in lst)
{
sb.Append(c.Text + "\r\n");
}
MessageBox.Show(sb.ToString());
}
Update:
You can even loop through the combobox direct within the panel without the help of list. You don't have to add it into List. Example:
void GetComboBoxValuesFromPanel()
{
StringBuilder sb = new StringBuilder();
foreach (Control c in panel1.Controls)
{
if (c is ComboBox)
{
ComboBox cb = (ComboBox)c;
sb.Append(cb.Text);
}
}
MessageBox.Show(sb.ToString());
}