VB Practical Assignment 3
VB Practical Assignment 3
public Form1()
{
InitializeComponent();
if (i == 1)
{
i = i + 1;
using (var openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "JSON files (*.json)|*.json|All files
(*.*)|*.*";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
var filePath = openFileDialog.FileName;
var fileContent = File.ReadAllText(filePath);
Data = fileContent;
var fileName = Path.GetFileName(filePath);
textBox1.Text = fileName;
}
}
} else
{
this.Hide();
Form2 form2 = new Form2();
form2.Show();
}
questions = quiz.Questions;
DisplayRandomQuestion();
this.button1.Click += new System.EventHandler(this.buttonOption_Click);
this.button2.Click += new System.EventHandler(this.buttonOption_Click);
}
}
private void DisplayRandomQuestion()
{
Random random = new Random();
int index = random.Next(questions.Count);
currentQuestion = questions[index];
richTextBox1.Text = currentQuestion.QuestionText;
button1.Text = currentQuestion.Options[0];
button2.Text = currentQuestion.Options[1];
}
if (selectedOption == currentQuestion.Answer)
{
MessageBox.Show(currentQuestion.CorrectFeedback, "Correct!",
MessageBoxButtons.OK);
wrongAnswersCount = 0;
}
else
{
MessageBox.Show(currentQuestion.WrongFeedback, "Wrong!",
MessageBoxButtons.OK);
wrongAnswersCount++;
}
if (wrongAnswersCount >= 3)
{
MessageBox.Show("You have answered incorrectly 3 times in a row. The
program will now terminate.", "Failure", MessageBoxButtons.OK);
Application.Exit();
}
else
{
DisplayRandomQuestion();
}
}
}
}
public class Quiz
{
[JsonProperty("questions")]
public List<Question> Questions { get; set; }
}
[JsonProperty("options")]
public List<string> Options { get; set; }
[JsonProperty("answer")]
public string Answer { get; set; }
[JsonProperty("correct_feadback")]
public string CorrectFeedback { get; set; }
[JsonProperty("wrong_feadback")]
public string WrongFeedback { get; set; }
public static string Data;: This variable stores the content of the selected JSON file as a string.
button1_Click: This event handler is triggered when the first button (button1) is clicked.
It checks if i is 1 (presumably to handle the first click and avoid repetitive file selection).
If i is 1, it creates an OpenFileDialog object.
This dialog allows the user to browse and select a JSON file.
If the user selects a file and clicks "OK" (DialogResult.OK), the code proceeds.
This code allows the user to select a JSON file containing quiz data using button1. The selected file's
content is then stored in the Data variable and potentially used by Form2 for the quiz functionality.
There's no code that modifies or terminates the original JSON file itself.
Classes:
Form2: This class represents the second form of the application which displays the
quiz questions and handles user interaction.
Fields:
questions: A list to store Question objects representing the quiz questions.
currentQuestion: A Question object to hold the currently displayed question.
wrongAnswersCount: An integer to track the number of consecutive incorrect
answers.
Constructor (Form2()):
Initializes the form components using InitializeComponent().
Retrieves the JSON data string from Form1.Data (assuming Form1 provides the data).
Deserializes the JSON string into a Quiz object using JsonConvert.DeserializeObject.
Extracts the questions list from the Quiz object and stores it in questions.
Calls DisplayRandomQuestion() to show the first question.
Attaches the buttonOption_Click event handler to both button1 and button2 for
handling option clicks.
Quiz: This class represents the overall quiz structure.
Field:
Questions: A list of Question objects representing the individual questions in the
quiz.
Question: This class represents a single question within the quiz.
Fields:
QuestionText: The text of the question.
Options: A list of strings containing the answer options.
Answer: The correct answer string.
CorrectFeedback: Feedback message displayed for a correct answer.
WrongFeedback: Feedback message displayed for an incorrect answer.
Methods:
Form2.DisplayRandomQuestion():
Creates a Random object.
Generates a random index within the questions list's range.
Sets currentQuestion to the question object at the generated index.
Updates the richTextBox1 text with the current question's text.
Sets the text of button1 and button2 to the first two options from
currentQuestion.Options.
Form2.buttonOption_Click(object sender, EventArgs e): (Event handler for both
option buttons)
Casts the sender object (the clicked button) to a Button.
Extracts the selected option text from the clicked button's text property.
Checks if the selected option matches the currentQuestion.Answer.
Correct Answer:
Displays a message box with the correctFeedback using MessageBox.Show.
Resets the wrongAnswersCount to 0.
Incorrect Answer:
Displays a message box with the wrongFeedback using MessageBox.Show.
Increments the wrongAnswersCount.
Checks if wrongAnswersCount is greater than or equal to 3:
If yes, displays a message box indicating termination and exits the
application using Application.Exit().
If no, calls DisplayRandomQuestion() to show a new question.
Overall Functionality:
The code creates a quiz application based on data provided in a JSON format. Users are
presented with randomly chosen questions from the list and select an answer from the
provided options. Feedback is provided based on the chosen answer. The application
terminates after three consecutive incorrect answers.