C# QUESTION: How to keep selected values of radio buttons and submit total score , [ in an exam quiz coming from database in a window form which has previous and next buttons like below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace exams
{
public partial class Form1 : Form
{
static int index = 0;
struct Question { public string question; }
struct Option { public string option1, option2, option3, option4; }
//new inclusion
static int Score = 0;
public Form1()
{
InitializeComponent();
GenerateQuestionsOptions(index);
btnNext.Click += new EventHandler(btnNext_Click);
btnPrevious.Click += new EventHandler(btnPrevious_Click);
btnSate();
}
void btnSate()
{
//new inclusion
if (index == 0) { btnPrevious.Visible = false; }
else { btnPrevious.Visible = true; }
int currentNo = index + 1;
if (currentNo < PopulateQuestions().Rows.Count) { btnNext.Visible = true; }
if (currentNo == PopulateQuestions().Rows.Count) { MessageBox.Show("This Question has Finished"); btnNext.Visible = false; }
}
void btnPrevious_Click(object sender, EventArgs e)
{
if (index > 0)
{
index--;
GenerateQuestionsOptions(index);
btnSate();
}
}
void btnNext_Click(object sender, EventArgs e)
{
if (index < PopulateQuestions().Rows.Count - 1)
{
index++;
GenerateQuestionsOptions(index, int.Parse(shuffledOptions.SelectedItem.Value));
btnSate();
}
}
public void GenerateQuestionsOptions(int index)
{
// Question based on Index
Question question = new Question();
question.question = PopulateQuestions().Rows[index]["question"].ToString();
// Options based on Question
Option options = new Option();
options.option1 = PopulateQuestions().Rows[index]["OptionOne"].ToString();
options.option2 = PopulateQuestions().Rows[index]["OptionTwo"].ToString();
options.option3 = PopulateQuestions().Rows[index]["OptionThree"].ToString();
options.option4 = PopulateQuestions().Rows[index]["OptionFour"].ToString();
// Adding options to List for shuffling options
List<string> optionsList = new List<string>();
optionsList.Add(options.option1);
optionsList.Add(options.option2);
optionsList.Add(options.option3);
optionsList.Add(options.option4);
// Shuffle options List
List<string> shuffledOptions = optionsList.OrderBy(a => Guid.NewGuid()).ToList();
// Assigning question and options
txtQuestion.Text = (index + 1) + " : " + question.question.ToUpper();
rbOption1.Text = shuffledOptions[0];
rbOption2.Text = shuffledOptions[1];
rbOption3.Text = shuffledOptions[2];
rbOption4.Text = shuffledOptions[3];
}
// Get question and options from database
private DataTable PopulateQuestions()
{
DataTable dt = new DataTable();
string constr = @"Server=localhost; database=questiondatabase; UID=root; password='password'";
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM tbl_questions"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
con.Close();
}
}
}
return dt;
}
}
}