Click here to Skip to main content
15,035,866 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Im making a quiz, c# windows form application, a login code, starting the quiz with 5 question

1 question ex:

what color is a ball?

radiobutton1 - white ( correct answer)

radiobutton2 - black

radiobutton3 - yellow

how can in the form when i presss a button next to tell me if i made a right answer, i have 5 forms for 5 question, and at the end form to count all the question for ex. you have made 3 correct and two wrong...

can anyone help me with this...


Exactly how you do this is difficult to say, since we have no idea how the rest of your system works. But, this is what I would do:
Create a storage medium that holds questions and answers: this could be a database, xml file, spreadsheet, or even a text file. Basically anything you are happy with that you can retrieve data from.
Each question would have a question text ("What colour is a ball?") and right answer ("White") and a number of wrong answers - this could be two, or you could have more and randomly select two of them.
Then, for each question, I'd use the Random class to give me a number and put the right answer in that radioButton.Text field - and set the Tag field of the control to "true". The other two radio buttons would get wrong answers, and a Tag of false.
When the button is pressed, find the selected radio button, and check it's Tag - if it's true they got it right.

There are many possible ways of implementation you could use here, and OriginalGriff has given you excellent suggestions.

A key question is: what is your goal here ? To create a one-off solution where all the logic and events happen in one Windows Form ? Or, to create a general solution that you can re-use in other projects, and to which you can add new features (extensibility) ?

Specific suggestions, whether your goal is one-off, or re-usable: don't use a separate Form for each question:

1. use a single Panel, or other ContainerControl, to hold the RadioButtons and other Controls you need, and re-use those Controls, changing their content for each question.

2. if you had additional content unique to each question (a picture ?), you could consider using a TabControl, with unique content for each question on each TabPage.

3. fyi: an alternative to checking one RadioButton at random is to set the Checked value of all RadioButtons to 'false at the start of each question. this may avoid a slight unconscious bias of the person taking the test to consider the checked value as being, possibly, more likely to be correct.

4. if your goal is a re-usable solution, consider using classes, to "model" both the collection of Questions/Answers, and individual Question/Answers. ask for help here with that, if you wish.
When user selects the answer and hits next button check for correct answer, if it is correct add a count to session and open next form, in next forms increment count if answer is correct.
1)instead of 5 forms you can change the text of the radiobutton and maintain a flag which question is to be shown.

2)to check how many correct answer declare 2 public variable and assign value on checked change of radio button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900