On click radio button checked without check answer button.

  • Thread starter Thread starter Programminggooru
  • Start date Start date
P

Programminggooru

Guest
Hello I am programminggooru,

I am running windows vista 32 bit with visual studio 2005 and C#, 2.0 .net. I am working on this multiple choice quiz or test. I am new to coding, but I started with this program in hopes that it will teach me a lot about coding in general. I found this website while checking for programming code with multiple choice question and answers. If you scroll down to the bottom of the website you will see the application area. Then you will see this Multiple choice question and answer with single answer code are program. I am a little lost on some of the code functionality, but would like to set this up. So I don’t have to use a button to check the answer of the radio button selected. On the button of the Form. Here is the code:



using System;

using System.Collections;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;



namespace DrivingTest1

{

public partial class QuestionAnswer : Form

{

public int TotalNumberOfQuestions;



// This variable will hold a list of randoom non-repeating numbers

ArrayList lstNumbers;

// This will represent the index of the question that is being asked

int CurrentQuestion;

// These will keep a count of the questions already asked

int QuestionCounter;

int CurrentNumberOfQuestions;

// This will be used to counte the number of correct answers

double Sum;

const int MaximumNumberOfQuestions = 20;

// This variable allows monitoring if a 0 had been

// added to the list of numbers;

bool Found0;



public QuestionAnswer()

{

InitializeComponent();

}

// This method takes a constant integer that represents

// the index of a question in the list of questions

// This method then displays the corresponding question

void PresentQuestion(int qstNumber)

{

rdoAnswer1.Checked = false;

rdoAnswer2.Checked = false;

rdoAnswer3.Checked = false;

rdoAnswer4.Checked = false;



switch (qstNumber)



case 1:

txtQuestion.Text = "If your car and a car coming from your " +

"right reach an intersection at the same " +

"time, who has the right of way?";



rdoAnswer1.Text = "A. Your car";

rdoAnswer2.Text = "B. The other car";

rdoAnswer3.Text = "C. You both wait for the first car that makes a move";

rdoAnswer4.Text = "D. Neither, as both cars must come to a stop";

//change the image in the picturebox for this question'

pictureBox1.Image = Image.FromFile(@"d:\\VS Electronic Test\\Resistor Test 1.jpg”);

break;



// This method takes as argument a constant integer

// The argument represents a question that was

// previously asked. The method checks the indexed

// question against its valid answer. If the answer

// to the question is valid, a total value is increased

void CheckAnswer(int qstNumber)

{

switch (qstNumber)

{

case 1:

if (rdoAnswer2.Checked == true)

{

MessageBox.Show("Correct");

Sum++;

}

else

MessageBox.Show("Wrong Answer");

break;





Here is the last part of the code with the checkAnswer button code are program:



private void btnCheckAnswer_Click(object sender, EventArgs e)

{

CurrentNumberOfQuestions++;

CheckAnswer(CurrentQuestion);



if (QuestionCounter >= lstNumbers.Count)

return;

CurrentQuestion = (int)(lstNumbers[QuestionCounter++]);



if (CurrentQuestion == 0)

{

Found0 = true;

CurrentQuestion = (int)(lstNumbers[QuestionCounter++]);

}



PresentQuestion(CurrentQuestion);

}



private void QuestionAnswer_Load(object sender, EventArgs e)

{

Found0 = false;

QuestionCounter = 0;



CurrentNumberOfQuestions = 0;



lstNumbers = new ArrayList();

Random rndNumber = new Random();



int number = rndNumber.Next(1, MaximumNumberOfQuestions + 1);

lstNumbers.Add(number);

int count = 0;



// Create a list of 20 non-repeating integers randomly chosen

do

{

// Randomly choose between 1 and the MaximumNumberOfQuestions

number = rndNumber.Next(0, MaximumNumberOfQuestions + 1);



// If the chosen number is not yet in the list,

// then add it

if (!lstNumbers.Contains(number))

{

lstNumbers.Add(number);

}// If the number is already in the list, ignore it



count++;

} while (count <= 100);



btnCheckAnswer_Click(sender, e);

}

}

}

Once you click on the radio button. Rdo Answer 1,2,3,or 4 it checks it with the other code in the case switch application. I was hoping someone can teach me how to check the radio button instead and get the response of correct or wrong. Instead of click on the CheckAnswer button.To revel the answer. Thanks on any help on this topic.

Continue reading...
 
Back
Top