locked
On click radio button checked without check answer button. RRS feed

  • Pregunta

  • 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 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.


    domingo, 2 de febrero de 2020 15:24

Respuestas

  • Hi Programminggooru,

    Thank you for posting here.

    According to your description, each question has only one answer, right?

    If so, we can bind the same radioButton_CheckedChanged event to all radiobuttons, then we can perform the desired operation after the user makes a selection.

    Here is the code:

    this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
    this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
    this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
    this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);


            private void radioButton_CheckedChanged(object sender, EventArgs e)
            {
                IEnumerable<RadioButton> buttons = this.Controls.OfType<RadioButton>();
                foreach (var item in buttons)
                {
                    if (item.Checked == true)
                    {
                        //do something...
                    }
                }
            }

    It should be noted that this is only suitable for questions with only one answer. If there are multiple answers, a confirmation button may be necessary.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    lunes, 3 de febrero de 2020 7:44

Todas las respuestas

  • Hi Programminggooru,

    Thank you for posting here.

    According to your description, each question has only one answer, right?

    If so, we can bind the same radioButton_CheckedChanged event to all radiobuttons, then we can perform the desired operation after the user makes a selection.

    Here is the code:

    this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
    this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
    this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
    this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);


            private void radioButton_CheckedChanged(object sender, EventArgs e)
            {
                IEnumerable<RadioButton> buttons = this.Controls.OfType<RadioButton>();
                foreach (var item in buttons)
                {
                    if (item.Checked == true)
                    {
                        //do something...
                    }
                }
            }

    It should be noted that this is only suitable for questions with only one answer. If there are multiple answers, a confirmation button may be necessary.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    lunes, 3 de febrero de 2020 7:44
  •  I am trying to run Timon's script and running into a compile error.

    this.Controls.OfType<RadioButton>();

    It is giving me a error message and also the var are variable option. I am not sure what I am doing

    wrong, but willing to learn. Thanks for any help on this topic. I appreciate it.


    miércoles, 5 de febrero de 2020 5:15
  • Thanks for your help Tim Yang. I was able to learn a lot by trial and error myself, which I am willing to do. Referring to compile errors in scripts. I would rather learn making mistakes while I learn coding. I hate to ask professional help to big coding problems. It's not fair to the professional party. I want to Thank the MSDN forum for the pushing me into this thinking and troubleshooting process in coding.

    I wanted to ask, if someone can help me by pointing me, in the right directions on this script. I am trying to eliminate the check answer button on the form and have it setup on Load_Form to check if the on click radioButton is selected. I have been working on this off and on in my spare time, since Feb. I am having fun so don't laugh. First things first, I forgot to include the link were I found the code that started my self-taught learning process in programming in the beginning.

    Here is the Link 

    http://www.functionx.com/vcsharp2003/applications/mcq1.htm

    Please Forgive me for not including this in my fist post. I also want to include a understanding. I am trying to build a C-Sharp program, so I can quiz myself on electronic questions. This will help keep my mind sharp on electronic principles.

               I believe, that I need to place Tim Yang's code void RadioButton_Checked in the Load_Form and to place the bottom part into the Check Answer script, which means re-writing the code to make it work. I just need someone, to check me to make sure I am on the right track. This will help me continue to learn coding, by troubleshooting myself. Thanks for any help.

     private void radioButton_CheckedChanged(object sender, EventArgs e)
            {
                IEnumerable<RadioButton> buttons = this.Controls.OfType<RadioButton>();
                foreach (var item in buttons)
                {
                    if (rdoAnswer2.Checked == true)
                    {
                        MessageBox.Show("Correct");
                    }
                    else
                        MessageBox.Show("Wrong Answer");
                
                
                }
            }
                
                        
                void CheckAnswer(int qstNumber)
    {
                switch (qstNumber)
                {
                    case 1:
                        if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;

                 I also forgot to state the first part of Tim's Code is perfect in the QuestionAnswer.Designer.cs. I have no compile errors so far. I just can't get a response on the new code. I have disable the btnCheckAnswer_Click, and selected Question's 1 answer, which is B. But the new code is not taking. I am still not sure were I am going wrong, but I am encourage by the fact I have got this far with no compile errors. Thanks again for any help.


    jueves, 27 de agosto de 2020 1:40
  •         I was able to figure this out and was able to make it work, but very unstable. I recommend leaving in the check answer button or submit button in c sharp radio button on click MCQ questions and answers. Now I have learned a valuable coding request and I see that the professionals know their stuff. Thanks for the help from Timon Yang.

    sábado, 29 de agosto de 2020 11:53