Hi!
I used below code for quiz its worked, but I have problem with timer for one question need 1 minuts.
By default rdOption must be not choosed when I choosed for first question answer and go to second question then first answer choosed show on next its must be not choosed for each question.
namespace QuesQuiz
{
public partial class Quiz : Form
{
public int i;
static int index = 0;
static int correct = 0;
static string answer = "";
public struct Question { public string question; }
public struct Option { public string optOne, optTwo, optThree, optFour, optFive; }
public Quiz()
{
InitializeComponent();
timer_quiz.Enabled = false;
timer_quiz.Interval = 6000;
i = 5;
grpBox.Visible = false;
}
private void btnStart_Click(object sender, EventArgs e)
{
timer_quiz.Start();
GenerateQuestionsOptions(index);
lblAnswer.Visible = false;
grpBox.Visible = true;
}
public void GenerateQuestionsOptions(int index)
{
// Question based on Index
Question question = new Question();
question.question = PopulateQuestions().Rows[index]["QuestionDescription"].ToString();
// Options based on Question
Option options = new Option();
options.optOne = PopulateQuestions().Rows[index]["OptionOne"].ToString();
options.optTwo = PopulateQuestions().Rows[index]["OptionTwo"].ToString();
options.optThree = PopulateQuestions().Rows[index]["OptionThree"].ToString();
options.optFour = PopulateQuestions().Rows[index]["OptionFour"].ToString();
options.optFive = PopulateQuestions().Rows[index]["OptionFive"].ToString();
// Answer based on Question
answer = PopulateCorrectAnswer(PopulateQuestions().Rows[index]["QuestionDescription"].ToString());
// Adding options to List for shuffling options
List<string> optionsList = new List<string>();
optionsList.Add(options.optOne);
optionsList.Add(options.optTwo);
optionsList.Add(options.optThree);
optionsList.Add(options.optFour);
optionsList.Add(options.optFive);
// Shuffle options List
List<string> shuffledOptions = optionsList.OrderBy(a => Guid.NewGuid()).ToList();
//List<string> shuffledOptions = RandomizeList(optionsList);
// Assigning question and options
lblQuestion.Text = (index + 1) + " : " + question.question.ToUpper();
rbOptionOne.Text = shuffledOptions[0];
rbOptionTwo.Text = shuffledOptions[1];
rbOptionThree.Text = shuffledOptions[2];
rbOptionFour.Text = shuffledOptions[3];
rbOptionFive.Text = shuffledOptions[4];
}
public List<string> RandomizeList(List<string> originalList)
{
List<string> randomList = new List<string>();
Random random = new Random();
string value = default(string);
while (originalList.Count() > 0)
{
var nextIndex = random.Next(0, originalList.Count());
value = originalList[nextIndex];
randomList.Add(value);
originalList.RemoveAt(nextIndex);
}
return randomList;
}
// Get question and options from database
private DataTable PopulateQuestions()
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM QuestionTable", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
con.Open();
da.Fill(dt);
con.Close();
}
}
}
return dt;
}
//Get answer for question from database
private string PopulateCorrectAnswer(string questionDescription)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("GetCorrectAnswer", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Question", questionDescription);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
con.Open();
da.Fill(dt);
con.Close();
}
}
}
return dt.Rows[0].ItemArray[0].ToString();
}
private void btnBack_Click(object sender, EventArgs e)
{
timer_quiz.Enabled = false;
timer_quiz.Interval = 60000;
i = 60;
index = 0;
GenerateQuestionsOptions(index);
lblAnswer.Visible = false;
btnBack.Visible = false;
btnStart.Visible = true;
}
private void timer_quiz_Tick(object sender, EventArgs e)
{
if(i > 0)
{
i = i - 1;
label1.Text = "0" + i.ToString();
foreach (Control control in grpBox.Controls)
{
if (control.GetType().Name.ToLower() == "radiobutton")
{
if ((control as RadioButton).Checked)
{
if (answer.ToLower() == (control as RadioButton).Text.ToLower())
{
correct++;
}
}
}
}
if (index < PopulateQuestions().Rows.Count - 1)
{
index++;
GenerateQuestionsOptions(index);
lblAnswer.Visible = false;
}
}
else
{
grpBox.Visible = false;
btnStart.Visible = false;
btnBack.Visible = true;
lblAnswer.Visible = true;
timer_quiz.Stop();
lblAnswer.Text = correct + " correct answer out of " + PopulateQuestions().Rows.Count + " question";
}
}
}
}