How do I compare a list of textboxes to a list in parallel?

I have a Math test program with 10 textboxes. The 10 textboxes are in a list and in the form load event I read in a txt file with the “answers” in order of each textbox. The “answers” txt is suppose to be in a list parallel to the textbox list.

So, my problem is that when you enter in your answer into a textbox it is suppose to compare what you typed in for the answer and what the answer actually is. Then add up how many you got right out of 10 and display it. It currently displays the wrong correct number of answers. It only takes the last textboxes entry and displays that number. So if I enter 100 is will display You answered 100 of 10 correctly. If you have any questions please let me know.

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace MathTestwithListsFreed
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<int> answernumbers = new List<int>();
        List<TextBox> listoftextboxes = new List<TextBox>();
        private void ExitButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listoftextboxes.Add(textBox01);
            listoftextboxes.Add(textBox02);
            listoftextboxes.Add(textBox03);
            listoftextboxes.Add(textBox04);
            listoftextboxes.Add(textBox05);
            listoftextboxes.Add(textBox06);
            listoftextboxes.Add(textBox07);
            listoftextboxes.Add(textBox08);
            listoftextboxes.Add(textBox09);
            listoftextboxes.Add(textBox10);
            try
            {
                StreamReader answerReader = File.OpenText("answers.txt");
                while (!answerReader.EndOfStream)
                {
                    answernumbers.Add(int.Parse((answerReader.ReadLine())));
                }
                answerReader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
            listoftextboxes.ForEach(txt => txt.Clear());
            
        }

        private void SubmitButton_Click(object sender, EventArgs e)
        {
            int correct = 0;
            for (int x = 0; x< listoftextboxes.Count; x++)
            {
                correct = int.Parse(listoftextboxes[x].Text);
            }
            MessageBox.Show("You answered " + correct.ToString() + " out of 10 correctly");
        }
    }
}

Answer

Firstly, you are overwriting correct on every run of the loop. The code you need there should probably be instead:

correct += int.Parse(listoftextboxes[x].Text);

Secondly, you are not actually using answernumbers to verify the answer. I would imagine what you actually want is something like this:

if (answernumbers[i] == listoftextboxes[x].Text)
    correct++;

Leave a Reply

Your email address will not be published. Required fields are marked *