What's about create a List<TextBox>
and then get a textbox using indexes and do the same thing with lenght using 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");
}
}
As said millimoose manage a parallel arrays could be hard and not the better solution.
So you can create a class like this:
class DataStructure
{
public TextBox Textbox
{
get;
set;
}
public double Lenght
{
get;
set;
}
public DataStructure(TextBox Textbox)
{
this.Textbox = Textbox;
}
}
Then always using a 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");
}
}