0

我有一组文本框和组合框,我想根据我要进行的循环设置它们的值。preinput1 和 postinput1 是文本框,但是 preinput5 和 postinput5 是组合框。试图将组合框称为文本框的错误:“TextBox tb”。

    var StringInput = new object[] { preInput1, preInput5, postInput1, postInput5};
    int stringCount1 = 0;
    int toto = (ArrayCount + StringInput.Length);

    foreach (TextBox tb in StringInput)
    {
      tb.Text = Convert.ToString(energyCalculation.Cells[place[xCSV]].Value);
      xCSV++;
      //stringCount1++;
      ArrayCount++;
    }

我想避免分解它来一一解决。我还能写什么来代替TextBox,或者有更好的方法。

干杯,

4

2 回答 2

2

尝试以下操作:

var StringInput = new Control[] { preInput1, preInput5, postInput1, postInput5};
int stringCount1 = 0;
int toto = (ArrayCount + StringInput.Length);

foreach (var c in StringInput)
{
  c.Text = Convert.ToString(energyCalculation.Cells[place[xCSV]].Value);
  xCSV++;
  //stringCount1++;
  ArrayCount++;
}
于 2013-03-23T07:31:27.220 回答
0

试试这个:

foreach (object control in StringInput)
{
  var value = Convert.ToString(energyCalculation.Cells[place[xCSV]].Value);

  var textBox = control as TextBox;
  if (textBox != null)
  {
    textBox.Text = value;
  }
  else
  {
    var comboBox = control as ComboBox;
    if (comboBox != null)
    {
      comboBox.Text = value;
    }
  }

  xCSV++;
  //stringCount1++;
  ArrayCount++;
}
于 2013-03-23T07:34:53.327 回答