0

我需要检查无限数量的文本框的文本和各种属性。我虽然我可以使用 {i} 以下面的方式做到这一点(所以它会检查 tbEavelength1,tbEavelength2, tbEavelength3 等)这不起作用想知道是否有人有任何想法?

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");
    }
}

提前感谢您的帮助!

4

3 回答 3

1

如何创建 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");
    }
}
于 2012-07-21T12:54:56.727 回答
0

如果您在文件后面的代码中,那么您可以使用方法FindName通过传递其名称来获取文本框的实例,然后可以像这样对该特定文本框执行操作 -

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");
    }
}
于 2012-07-21T20:40:58.930 回答
0

您好,您可以尝试使用 FindControl() 方法查找文本框。我假设您正在处理 Asp.net 页面。

例如。

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");
    }
 }
于 2012-07-21T11:58:15.563 回答