Click here to Skip to main content
15,885,188 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello!

I am writing a quiz program in C# and I'm at a bit of a loss on how I can compare the user's answers to the answer key. Basically, I want there to be 2 arrays (if my method is inefficient please suggest another way) one which stores the answer key and one which stores the user's input. So I want the user to type in their answers into textboxes and then the program will compare the textbox's text to the answer key... Basically like this:
C#
if (textBoxes[i].Text == answerKey[i]) { correct++; }
else { wrong++; }

Where, the correct and wrong variables keep track of how many questions the user got right and wrong and the answerKey array stores all the answers.

So my question is, how can I take all of the textboxes inside of my form and place them into the textbox array?
Posted
Comments
Karthik_Mahalingam 29-Jan-14 20:15pm    
post full code
Ahsan Lopez 29-Jan-14 20:21pm    
Why would you need that? All I need to find out is how to take all the textboxes on my Form and put them into an array which I can compare with another array.
Karthik_Mahalingam 29-Jan-14 20:28pm    
how many textboxes ?
what names you have used for that..
Karthik_Mahalingam 29-Jan-14 20:28pm    
web or windows ?
Ahsan Lopez 29-Jan-14 20:34pm    
Desktop on Windows. A lot of textboxes which is why I don't want to use individual if statements for all of them. I left the default textBox1, textBox2... etc names for them.

Try this..

C#
int countAnswers = 0;
            List<TextBox> lstText =new List<TextBox> ();
            var controls = this.Controls.OfType<TextBox>().ToArray();
            for (int i = 0; i < controls.Length; i++)
            {
                if (controls[i].Name == "textBox" + i)
                    lstText.Add(controls[i]);
            }
            // make sure that the lstText has only 10 items

            string[] answers = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
            for (int i = 0; i < 10; i++)
            {
                if (lstText[i].Text == answers[i])
                    countAnswers ++;
            }
            int countWrong = lstText.Count - countAnswers;
 
Share this answer
 
v2
Here's one possibility.
C#
// create list to hold textbox controls; our form contains four textbox controls
List<TextBox> textBoxes = new List<TextBox>();
textBoxes.Add(textBox1);
textBoxes.Add(textBox2);
textBoxes.Add(textBox3);
textBoxes.Add(textBox4);            

// iterate list and check textbox text against the answer keys
foreach (TextBox tb in textBoxes)
{
    if (answerKey.Contains(tb.Text)) { correct++; }
    else { wrong++; }                
}
 
Share this answer
 
v2
Comments
Ahsan Lopez 29-Jan-14 20:59pm    
Thank you for your solution, however because I have almost 100 textboxes, adding all of them one by one into a List does not seem very efficient.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900