如何创建 aList<TextBox>
然后使用索引获取文本框并使用 lenght 执行相同的操作 a List<double>
?
//List<TextBox> listTextBoxes = new List<TextBox>();
//populate the list of textboxes
//List<double> listEaveLength = new List<double>();
for (int i = 1; i <= comboBox1.SelectedIndex + 1; i++)
{
if (listTextBoxes[i].IsEnabled == false)
{
listEaveLength[i] = 0;
}
else if (listTextBoxes[i].Text == "")
{
throw new Exception(listTextBoxes[i].Name + " must have a value");
}
else if (!double.TryParse(listTextBoxes[i].Text, out listEaveLength[i]))
{
throw new Exception(listTextBoxes[i].Name + " must be numerical");
}
}
如前所述,毫驼管理并行阵列可能很困难,而不是更好的解决方案。所以你可以像这样创建一个类:
class DataStructure
{
public TextBox Textbox
{
get;
set;
}
public double Lenght
{
get;
set;
}
public DataStructure(TextBox Textbox)
{
this.Textbox = Textbox;
}
}
然后总是使用List<DataStructure>
:
//List<DataStructure> myList = new LList<DataStructure>();
//myList.Add(new DataStructure(myTextBox));
//... populate your list
for (int i = 1; i <= comboBox1.SelectedIndex + 1; i++)
{
if (myList[i].Textbox.IsEnabled == false)
{
myList[i].Lenght = 0;
}
else if (myList[i].Textbox.Text == "")
{
throw new Exception(myList[i].Textbox.Name + " must have a value");
}
else if (!double.TryParse(myList[i].Textbox.Text, out myList[i].Lenght))
{
throw new Exception(myList[i].Textbox.Name + " must be numerical");
}
}