what
what

Reputation: 373

Accessing textbox controls in foreach loop

I have 2 list string like this :

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

answerList is already added with string inside choiceList is the list where i want to add the text of each textboxes in the panel

I have many textboxes in a panel and each textbox , user input text into each textbox then when user click Check button , i want to do a foreach loop to access the loop controls .

So i want to do a foreach loop to loop the textboxes controls text and compare it with answerList , and if it is different , it will change the textbox background color.

I manage to do until here ( it doesn't even enter the IF statement ) :

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

FYI , textboxes are dynamically created and added to Panels .

I need help as i did this before but i forgotten how been a long time since i did programming ...

EDIT


The rest of my code ( partial , the way i create my controls and add them to panel ) :

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

    }

Upvotes: 1

Views: 3118

Answers (4)

gmail user
gmail user

Reputation: 2783

To compare 2 List, you can also use this

        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
            }

        }

Upvotes: 0

Abbas
Abbas

Reputation: 14432

You can still use the way you already did, only use is to see if it is indeed a 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());
        }                       
    }
}

Or you can use LinQ's OfType<T> method to skip the if statment:

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

More reading:

Edit:

If the textboxes are evaluated in the same order as the values in the answerlist, you can compare the value after you added the item to the choicelist. Your code will look like:

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

But if you work this way and you don't need the answers afterwards anymore, you can just skip the part of adding the value to the choiceList. And then you can work with a counter:

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

If the textboxes are not evaluated in the same order you can use Contains to see if a choice is in the answerlist:

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

Upvotes: 2

Marek
Marek

Reputation: 3575

Try

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

Upvotes: 0

Florin V
Florin V

Reputation: 344

Try with

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

}

Upvotes: 0

Related Questions