SuperManSL
SuperManSL

Reputation: 1306

Get values of dynamically created controls (comboboxes)

I've got panel on which by default are two comboboxes and one "+" button which creates two new combo boxes bellow the first one, I can create multiple (n) rows with two combo boxes and everything is working, I just can't figure out how to get values of those boxes?

Here's code for creating (adding) controls

private void btnCreateFilter_Click(object sender, EventArgs e)
{

    y += comboBoxHeight;
    ComboBox cb = new ComboBox();
    cb.Location = new Point(x, y);
    cb.Size = new Size(121, 21);

    panelFiltri.Controls.Add(cb);

    yDrugi += comboBoxHeight;
    ComboBox cbSql = new ComboBox();
    cbSql.Location = new Point(xDrugi, yDrugi);
    cbSql.Size = new Size(121, 21);
    panelFiltri.Controls.Add(cbSql);

    btnCancel.Location = new Point(btnCancel.Location.X, btnCancel.Location.Y + 25);
    btnSaveFilter.Location = new Point(btnSaveFilter.Location.X, btnSaveFilter.Location.Y + 25);
} 

And here's code where I'm lost:

 private void btnSaveFilter_Click(object sender, EventArgs e)
{
    int i;
    foreach (Control s in panelFiltri.Controls)
    {

       //GOT LOST

    }
}

Upvotes: 0

Views: 2869

Answers (2)

Oskar Birkne
Oskar Birkne

Reputation: 813

You can get the text in the ComboBox as

private void btnSaveFilter_Click(object sender, EventArgs e)
{
    foreach (Control control in panelFiltri.Controls)
    {
        if (control is ComboBox)
        {
            string valueInComboBox = control.Text;
            // Do something with this value
        }
    }
}

Upvotes: 1

stefankmitph
stefankmitph

Reputation: 3306

I don't really know what you're trying to achieve... Maybe this will help you along...

private void btnSaveFilter_Click(object sender, EventArgs e)
{
  foreach (ComboBox comboBox in panelFiltri.Controls)
  {  
     var itemCollection = comboBox.Items;
     int itemCount = itemCollection.Count; // which is 0 in your case
  }
}

Upvotes: 0

Related Questions