0

我有一个面板,默认情况下是两个组合框和一个“+”按钮,它在第一个组合框下面创建两个新组合框,我可以用两个组合框创建多 (n) 行,一切正常,我只是可以不知道如何获取这些框的值?

这是创建(添加)控件的代码

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);
} 

这是我迷路的代码:

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

       //GOT LOST

    }
}
4

2 回答 2

1

您可以将 ComboBox 中的文本作为

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
        }
    }
}
于 2013-04-30T07:51:45.183 回答
0

我真的不知道你想要达到什么目标......也许这会帮助你......

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
  }
}
于 2013-04-30T07:46:52.137 回答