在同一个foreach循环中设置combobox和texbox的值
本文关键字:combobox texbox 的值 设置 同一个 foreach 循环 | 更新日期: 2023-09-27 18:05:30
我有一个数组的文本框和组合框,我想设置他们的值对循环我已经走了。preinputt1和postinputt1是文本框,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,或者有更好的方法。
欢呼,
尝试如下:
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++;
}
试试这个:
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++;
}