访问foreach循环中的文本框控件

时间:2014-01-28 13:06:55

标签: c# string list foreach textbox

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

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

answerList已经添加了字符串 choiceList是我想在面板中添加每个文本框的文本的列表

我在面板和每个文本框中都有很多文本框,用户在每个文本框中输入文本 然后当用户点击Check按钮时,我想做一个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 .
        }                       
    }
}

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

我之前需要帮助,但我忘记了自编程以来已经很长时间了......

编辑


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

  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());

    }

4 个答案:

答案 0 :(得分:2)

你仍然可以使用你已经采用的方式,只使用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<T>方法跳过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;
    }
}

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

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

答案 1 :(得分:0)

尝试

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

}

答案 2 :(得分:0)

尝试

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
    }
}

答案 3 :(得分:0)

要比较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
            }

        }
相关问题