public partial class HistoryQuizForm : Form
{
private int currentQuestion = 0;
private int score = 0;
private List<Question> questions = new List<Question>()
{
new Question("When did World War II end?", new List<string>() {"1945", "1939", "1941", "1943"}, "1945"),
new Question("Who was the first president of the United States?", new List<string>() {"Thomas Jefferson", "George Washington", "John Adams", "Abraham Lincoln"}, "George Washington"),
new Question("What year was the United Nations founded?", new List<string>() {"1935", "1945", "1947", "1950"}, "1945")
};
public HistoryQuizForm()
{
InitializeComponent();
DisplayQuestion();
}
private void DisplayQuestion()
{
Question question = questions[currentQuestion];
questionLabel.Text = question.Text;
answerAButton.Text = question.Answers[0];
answerBButton.Text = question.Answers[1];
answerCButton.Text = question.Answers[2];
answerDButton.Text = question.Answers[3];
}
private void CheckAnswer(string selectedAnswer)
{
Question question = questions[currentQuestion];
if (question.CorrectAnswer == selectedAnswer)
{
score++;
feedbackLabel.Text = "Correct!";
feedbackLabel.ForeColor = Color.Green;
}
else
{
feedbackLabel.Text = "Incorrect!";
feedbackLabel.ForeColor = Color.Red;
}
// Disable answer buttons
answerAButton.Enabled = false;
answerBButton.Enabled = false;
answerCButton.Enabled = false;
answerDButton.Enabled = false;
// Show next button
nextButton.Visible = true;
}
private void answerAButton_Click(object sender, EventArgs e)
{
CheckAnswer(answerAButton.Text);
}
private void answerBButton_Click(object sender, EventArgs e)
{
CheckAnswer(answerBButton.Text);
}
private void answerCButton_Click(object sender, EventArgs e)
{
CheckAnswer(answerCButton.Text);
}
private void answerDButton_Click(object sender, EventArgs e)
{
CheckAnswer(answerDButton.Text);
}
private void nextButton_Click(object sender, EventArgs e)
{
currentQuestion++;
if (currentQuestion == questions.Count)
{
// Quiz is over
MessageBox.Show($"Quiz complete! You scored {score}/{questions.Count}.");
this.Close();
}
else
{
// Load next question
DisplayQuestion();
feedbackLabel.Text = "";
feedbackLabel.ForeColor = Color.Black;
// Enable answer buttons
answerAButton.Enabled = true;
answerBButton.Enabled = true;
answerCButton.Enabled = true;
answerDButton.Enabled = true;
// Hide next button
nextButton.Visible = false;
}
}
}