0

I need to check text and various properties of unlimited amount of textboxes. I though i might be able to do it the way below using {i} (so it would check through tbEavelength1,tbEavelength2, tbEavelength3 etc. ) this doesn't work was wondering if anyone had any ideas?

for (int i = 1; i <= comboBox1.SelectedIndex + 1; i++)
{
    if (tbEaveLength{i}.IsEnabled == false)
    {
        eaveLength{i} = 0;
    }
    else if (tbEaveLength{i}.Text == "")
    {
        throw new Exception("EaveLength {i} must have a value");
    }
    else if (!double.TryParse(tbEaveLength{i}.Text, out eaveLength{i}))
    {
       throw new Exception("EaveLength {i} must be numerical");
    }
}

Thanks in advance for any help!

Omar
  • 16,329
  • 10
  • 48
  • 66
John Churchley
  • 434
  • 4
  • 17

3 Answers3

1

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");
    }
}
Omar
  • 16,329
  • 10
  • 48
  • 66
0

Hi you can try to find the textbox by using FindControl() method. I assume you are working on Asp.net page.

Eg.

for (int i = 1; i <= comboBox1.SelectedIndex + 1; i++)
{
    var tbEaveLength = FindControl("tbEaveLength" + i);
    if (tbEaveLength.IsEnabled == false)
    {
        eaveLength = 0;
    }
    else if (tbEaveLength.Text == "")
    {
        throw new Exception("EaveLength {i} must have a value");
    }
    else if (!double.TryParse(tbEaveLength{i}.Text, out eaveLength{i}))
    {
        throw new Exception("EaveLength {i} must be numerical");
    }
 }
ysrb
  • 6,693
  • 2
  • 29
  • 30
0

If you are in your code behind file, then you can use method FindName to get the instance of a textbox by passing its name and then can perform operation on that particular textbox like this -

for (int i = 1; i <= comboBox1.SelectedIndex + 1; i++)
{
    TextBox textBox = (TextBox)FindName("tbEaveLength" + i);
    if (textBox.IsEnabled == false)
    {
        eaveLength{i} = 0;
    }
    else if (textBox.Text == "")
    {
        throw new Exception("EaveLength {i} must have a value");
    }
    else if (!double.TryParse(textBox.Text, out eaveLength{i}))
    {
       throw new Exception("EaveLength {i} must be numerical");
    }
}
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185