【问题标题】:Accessing textbox controls in foreach loop在 foreach 循环中访问文本框控件
【发布时间】:2014-02-19 19:08:30
【问题描述】:

我有 2 个这样的列表字符串:

List<string> answerList = new List<string>();
List<string> choiceList = new List<string>();

answerList 已添加,其中包含字符串 choiceList 是我要在面板中添加每个文本框的文本的列表

我在面板和每个文本框中有许多文本框,用户在每个文本框中输入文本 然后当用户单击检查按钮时,我想做一个 foreach 循环来访问循环控件。

所以我想做一个 foreach 循环来循环文本框控件文本并将其与 answerList 进行比较,如果不同,它将更改文本框背景颜色。

我设法做到了直到这里(它甚至没有进入 IF 语句):

protected void btnCheck_Click(object sender, EventArgs e)
{
    foreach (Control s in Panel1.Controls)
    {
        //it won't enter here.
        if (s.GetType() == typeof(TextBox))
        {
            TextBox tb = s as TextBox;
            choiceList.Add(tb.Text.Trim());

           //Compare here but i don't know how .
        }                       
    }
}

仅供参考,文本框是动态创建并添加到 Panels 的。

我需要帮助,因为我以前这样做过,但我忘记了我有多久没有编程了......

编辑


我的其余代码(部分,我创建控件并将它们添加到面板的方式):

  dr = cmd.ExecuteReader();

    while (dr.Read())
    {
        Label question = new Label();
        question.Text = dr["question"].ToString();
        Panel1.Controls.Add(question);

        LiteralControl lc = new LiteralControl();
        lc.Text = "&nbsp&nbsp&nbsp&nbsp";
        Panel1.Controls.Add(lc);

        TextBox answer = new TextBox();
        answer.Width = 100;
        Panel1.Controls.Add(answer);

        LiteralControl lc1 = new LiteralControl();
        lc1.Text = " <br />";
        Panel1.Controls.Add(lc1);

        answerList.Add(dr["answer"].ToString());

    }

【问题讨论】:

  • 这个Panel中有Controls吗?你能查一下Controlscount吗?
  • 当我检查foreach循环的控件时,它只循环一次@AnatoliiGabuza

标签: c# string list foreach textbox


【解决方案1】:

你仍然可以使用你已经做的方式,只使用is 看看它是否确实是一个TextBox:

protected void btnCheck_Click(object sender, EventArgs e)
{
    foreach (Control s in Panel1.Controls)
    {
        if (s is TextBox)
        {
            TextBox tb = (TextBox)s;
            choiceList.Add(tb.Text.Trim());
        }                       
    }
}

或者你可以使用 LinQ 的 OfType&lt;T&gt; 方法跳过 if 语句:

protected void btnCheck_Click(object sender, EventArgs e)
{
    foreach (TextBox t in Panel1.Controls.OfType <TextBox>())
    {
        choiceList.Add(t.Text.Trim());
    }
}

更多阅读:

编辑:

如果文本框的评估顺序与答案列表中的值相同,您可以在将项目添加到选择列表后比较值。您的代码将如下所示:

foreach (TextBox t in Panel1.Controls.OfType <TextBox>())
{
    string tmp = t.Text.Trim();
    choiceList.Add(tmp);

    if(answerList[choiceList.Count-1] != tmp)
    {
        //Change background-color of t
        t.BackColor = Color.Red;
    }
}

但是,如果您以这种方式工作并且之后不再需要答案,则可以跳过将值添加到选择列表的部分。然后你可以使用计数器:

int i = 0;

foreach (TextBox t in Panel1.Controls.OfType <TextBox>())
{
    if(answerList[i++] != t.Text.Trim())
    {
        //Change background-color of t
        t.BackColor = Color.Red;
    }
}

如果文本框的评估顺序不同,您可以使用Contains 查看答案列表中是否有选项:

if(!answerList.Contains(t.Text.Trim())
    //Change background-color

【讨论】:

  • 问题是没有进入s.gettype = textbox的循环
  • 你试过我的代码了吗?我不使用s.GetType() == typeof(TextBox)
  • 是不是因为我在面板中还有其他控件?
  • 试试我的代码没关系。在我的代码中,我只会遍历 TextBox 类型的控件,试试吧。
【解决方案2】:

试试

foreach (TextBox t in Panel1.Controls.OfType <TextBox>())
{
  if(!answerList.Contains(t.text)
  {
       t.BackColor= Color.Red;
  }

}

【讨论】:

    【解决方案3】:

    试试

    List<string> notInanswerList = new List<string>();
    foreach (textbox t in Panel1.Controls.OfType<TextBox>())
    {
        choiceList.Add(t.Text.Trim());
        if (!answerList.Contain(t.Text.Trim()))
        {
            notInanswerList.Items.Add(t.Text.Trim());
            t.BackColor = System.Drawing.Color.Red; // changes the textBox BackColor
        }
    }
    

    【讨论】:

      【解决方案4】:

      比较2个List,也可以用这个

              List<string> questions = new List<string>() { "a", "b", "c", "d", "e", "f" };
              List<string> answers = new List<string>() { "b","c","d","x"};
      
              foreach (string str in answers)
              {
                  if (questions.Exists(q => q == str))
                  {
                      Console.WriteLine("answer found " + str); //print b,c,d
                  }
                  else
                  {
                      Console.WriteLine("answer not found " + str); //print x
                  }
      
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-11
        相关资源
        最近更新 更多