You should use the debugger and check the tag value. Also instead of a case, you should use a list of the questions and answers (data driven code)
Having issues with Visual Basic C# Quiz

I'm having an issue with my code for my college course. I've been trying to find the fix all day and I can't seem to resolve it. I have to create a VB quiz platform for primary school students. In one of my sections (Geography) I have a flag quiz button. In this section 12 questions are asked each with the question "What country uses this flag?" or something along those lines. There are 4 buttons beneath, these buttons display corresponding answers, with one being the correct answer. For some reason It does not count or collect correct answers and when displayed at the end no results are shown.
me and my tutor are completely stummped. A VB master is required for this one 😂. Any help is great. The video tutorial I used and My code for the quiz is below:
https://www.youtube.com/watch?v=hQDjz2ISklw&t=2121s
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;
namespace Unit_4_Assignment_2_Program
{
public partial class Form1 : Form
{
//Quiz Vars
int correctAnswer;
int questionNumber = 1;
int score;
int percentage;
int totalquestions;
public Form1()
{
InitializeComponent();
askQuestion(questionNumber);
totalquestions = 12;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void checkAnswerEvent(object sender, EventArgs e)
{
var senderObject = (Button)sender;
int buttonTag = Convert.ToInt32(senderObject.Tag);
if(buttonTag == correctAnswer)
{
score++;
}
if(questionNumber == totalquestions)
{
//Percentage
percentage = (int)Math.Round((double)(score * 100) / totalquestions);
MessageBox.Show(
"Quiz Concluded!" + Environment.NewLine +
"You have Answered " + score + " questions correctly." + Environment.NewLine +
"Your total Percentage is " + percentage + "%" + Environment.NewLine +
"Click OK to play again."
);
score = 0;
questionNumber = 0;
askQuestion(questionNumber);
}
questionNumber++;
askQuestion(questionNumber);
}
private void askQuestion(int qnum)
{
switch (qnum)
{
case 1:
pictureBox1.Image = Properties.Resources.UK;
Question.Text = "What country's flag is this?";
button1.Text = "Canada";
button2.Text = "UK";
button3.Text = "USA";
button4.Text = "India";
correctAnswer = 2;
break;
case 2:
pictureBox1.Image = Properties.Resources.USA;
Question.Text = "What country's flag is this?";
button1.Text = "England";
button2.Text = "Russia";
button3.Text = "Canada";
button4.Text = "USA";
correctAnswer = 4;
break;
case 3:
pictureBox1.Image = Properties.Resources.SPA;
Question.Text = "What country's flag is this?";
button1.Text = "Denmark";
button2.Text = "Spain";
button3.Text = "Ireland";
button4.Text = "Portugal";
correctAnswer = 2;
break;
case 4:
pictureBox1.Image = Properties.Resources.RUS;
Question.Text = "What country's flag is this?";
button1.Text = "Russia";
button2.Text = "USA";
button3.Text = "England";
button4.Text = "China";
correctAnswer = 1;
break;
case 5:
pictureBox1.Image = Properties.Resources.ITA;
Question.Text = "What country's flag is this?";
button1.Text = "Germany";
button2.Text = "France";
button3.Text = "Norway";
button4.Text = "Italy";
correctAnswer = 4;
break;
case 6:
pictureBox1.Image = Properties.Resources.IRE;
Question.Text = "What country's flag is this?";
button1.Text = "Ireland";
button2.Text = "Scotland";
button3.Text = "England";
button4.Text = "Wales";
correctAnswer = 1;
break;
case 7:
pictureBox1.Image = Properties.Resources.IND;
Question.Text = "What country's flag is this?";
button1.Text = "Pakistan";
button2.Text = "Japan";
button3.Text = "India";
button4.Text = "China";
correctAnswer = 3;
break;
case 8:
pictureBox1.Image = Properties.Resources.GER;
Question.Text = "What country's flag is this?";
button1.Text = "Austria";
button2.Text = "Australia";
button3.Text = "Russia";
button4.Text = "Germany";
correctAnswer = 4;
break;
case 9:
pictureBox1.Image = Properties.Resources.FRE;
Question.Text = "What country's flag is this?";
button1.Text = "Germany";
button2.Text = "France";
button3.Text = "Ukraine";
button4.Text = "Ireland";
correctAnswer = 2;
break;
case 10:
pictureBox1.Image = Properties.Resources.CHI;
Question.Text = "What country's flag is this?";
button1.Text = "China";
button2.Text = "Japan";
button3.Text = "Russia";
button4.Text = "Australia";
correctAnswer = 1;
break;
case 11:
pictureBox1.Image = Properties.Resources.CAN;
Question.Text = "What country's flag is this?";
button1.Text = "Poland";
button2.Text = "USA";
button3.Text = "UK";
button4.Text = "Canada";
correctAnswer = 4;
break;
case 12:
pictureBox1.Image = Properties.Resources.AST;
Question.Text = "What country's flag is this?";
button1.Text = "Germany";
button2.Text = "China";
button3.Text = "UK";
button4.Text = "Australia";
correctAnswer = 4;
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Home_Click(object sender, EventArgs e)
{
Home f3 = new Home();
f3.Show();
this.Hide();
this.Dispose();
}
}
}
Developer technologies | C#
2 answers
Sort by: Most helpful
-
Bruce (SqlWork.com) 77,926 Reputation points Volunteer Moderator
2023-04-25T15:16:54.8766667+00:00 -
Anonymous
2023-04-27T09:47:33.06+00:00 Hi @Bingo still , welcome to Microsoft Q&A.
You can create a Question class that contains questions, pictures, answers, options.
Then create a list and put all the questions in it.
Create corresponding variables, and use the checkquestion event and askQuestion method as you do.
When the question is finished, the score is announced and reset.
Code as follow:
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp7 { public partial class Form1 : Form { private class Question { public string QuestionInformation { get; set; } public string Answer { get; set; } public Image Image { get; set; } public string button1_text { get; set; } public string button2_text { get; set; } public string button3_text { get; set; } public string button4_text { get; set; } public Question(string questionInformation, string answer, Image image, string button1_text, string button2_text, string button3_text, string button4_text) { QuestionInformation = questionInformation; Answer = answer; Image = image; this.button1_text = button1_text; this.button2_text = button2_text; this.button3_text = button3_text; this.button4_text = button4_text; } } private int Score = 0; private int QuestionNumber = 0; private string AnswerN = null; private List<Question> questions = new List<Question>(); public Form1() { InitializeComponent(); questions.Add(new Question("q1", "demo1", null, "demo1", "demo2", "demo3", "demo4")); questions.Add(new Question("q2", "demo2", null, "demo2", "demo1", "demo3", "demo4")); questions.Add(new Question("q3", "demo3", null, "demo4", "demo2", "demo3", "demo1")); questions.Add(new Question("q4", "demo1", null, "demo1", "demo3", "demo2", "demo4")); button1.Click += checkAnswerEvent; button2.Click += checkAnswerEvent; button3.Click += checkAnswerEvent; button4.Click += checkAnswerEvent; askQuestion(QuestionNumber); } private void checkAnswerEvent(object sender, EventArgs e) { Button button = sender as Button; if (button.Text == AnswerN) { Score++; } if (QuestionNumber < questions.Count - 1) { askQuestion(++QuestionNumber); } else { //Percentage int totalquestions = questions.Count; int percentage = (int)Math.Round((double)(Score * 100) / totalquestions); MessageBox.Show( "Quiz Concluded!" + Environment.NewLine + "You have Answered " + Score + " questions correctly." + Environment.NewLine + "Your total Percentage is " + percentage + "%" + Environment.NewLine + "Click OK to play again." ); Score = 0; QuestionNumber = 0; askQuestion(QuestionNumber); } } private void askQuestion(int qnum) { pictureBox1.Image = questions[qnum].Image; label1.Text = questions[qnum].QuestionInformation; button1.Text = questions[qnum].button1_text; button2.Text = questions[qnum].button2_text; button3.Text = questions[qnum].button3_text; button4.Text = questions[qnum].button4_text; AnswerN = questions[qnum].Answer; } } }
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.