T
T
teilzteilzteilzteilz2021-08-23 15:35:29
C++ / C#
teilzteilzteilzteilz, 2021-08-23 15:35:29

What to do if switch/case does not work when combined with radioButton elements?

When creating a testing system, I ran into a problem. With the help of the switch / case method, I decided to create questions with the choice of answers, using the "radioButton" elements (added 4 radioButtons for 1 question).

When I started debugging for error checking and started doing checked on these radio buttons, the looping began. For example, if I click on 1 radio button in one question, it will remain so until the end of the test when switching to other cases with questions, it will be checked, and if in another question I suddenly click, for example, on the 3rd radio button, it will be checked when returning to the previous case'y will give me the 3rd one, although I put the 1st one on the previous questions.

What do I need to do to avoid this "looping" on any of the radio buttons? So that if I chose the 1st radio button in the 4th question, it would still be checked on the 4th question or on the 2nd question, the 2nd radio button is checked. I tried to reset the radiobuttons, but nothing happened.

namespace ProgrammName
{
    public partial class Form3 : Form
    {
        int n = 0;
        int[] answer;
        int correct = 0;
        public Form3()
        {
            InitializeComponent();
            answer = new int[19];
        }

        public void show(int n)
        {
            int n1 = n + 1;
            label1.Text = "Вопрос" + " " + n1;
            switch (n)
            {
                //Вопрос 1
                case 0:
                    pictureBox1.Image = new Bitmap(ProgrammName.Properties.Resources.t1);
                    label2.Text = "";
                    radioButton1.Text = "60 000";
                    radioButton2.Text = "20 000";
                    radioButton3.Text = "80 000";
                    if (radioButton3.Checked == true)
                    {
                        correct++;
                    }
                    radioButton4.Text = "70 000";
                    pictureBox1.Visible = true;
                    pictureBox2.Visible = false;
                    break;
                //Вопрос 2
                case 1:
                    label2.Text = "";
                    radioButton1.Text = "20";
                    radioButton2.Text = "8";
                    radioButton3.Text = "10";
                    radioButton4.Text = "5";
                    pictureBox1.Visible = false;
                    pictureBox2.Visible = false;
                    answer[n] = 1;
                    break;
                //Вопрос 3
                case 2:
                    label2.Text = "";
                    radioButton1.Text = "30";
                    radioButton2.Text = "3";
                    radioButton3.Text = "12";
                    radioButton4.Text = "6";
                    pictureBox1.Visible = false;
                    pictureBox2.Visible = false;
                    answer[n] = 3;
                    break;
                //Вопрос 4
                case 3:
                    pictureBox1.Image = new Bitmap(ProgrammName.Properties.Resources.t4);
                    label2.Text = "";
                    radioButton1.Text = "8";
                    radioButton2.Text = "13";
                    radioButton3.Text = "5";
                    radioButton4.Text = "2";
                    pictureBox1.Visible = false;
                    pictureBox2.Visible = false;
                    answer[n] = 2;
                    break;
                //Вопрос 5
                case 4:
                    pictureBox1.Image = new Bitmap(ProgrammName.Properties.Resources.t5);
                    label2.Text = "";
                    radioButton1.Text = "4й";
                    radioButton2.Text = "7й";
                    radioButton3.Text = "3й";
                    radioButton4.Text = "5й";
                    pictureBox1.Visible = true;
                    pictureBox2.Visible = false;
                    answer[n] = 2;
                    break;
                //Вопрос 6
                case 5:
                    pictureBox1.Image = new Bitmap(ProgrammName.Properties.Resources.t6);
                    pictureBox2.Image = new Bitmap(ProgrammName.Properties.Resourcest6_1);
                    label2.Text = "";
                    radioButton1.Text = "10";
                    radioButton2.Text = "5";
                    radioButton3.Text = "8";
                    radioButton4.Text = "20";
                    pictureBox1.Visible = true;
                    pictureBox2.Visible = true;
                    break;
                //Вопрос 7
                case 6:
                    label2.Text = "";
                    radioButton1.Text = "4";
                    radioButton2.Text = "2";
                    radioButton3.Text = "8";
                    radioButton4.Text = "6";
                    pictureBox1.Visible = false;
                    pictureBox2.Visible = false;
                    break;
                    
                //Вопрос 8
                case 7:
                    label2.Text = "";
                    radioButton1.Text = "21";
                    radioButton2.Text = "12";
                    radioButton3.Text = "14";
                    radioButton4.Text = "26";
                    pictureBox1.Visible = false;
                    pictureBox2.Visible = false;
                    break;
                //Вопрос 9
                case 8:
                    label2.Text = "";
                    radioButton1.Text = "2";
                    radioButton2.Text = "3";
                    radioButton3.Text = "8";
                    radioButton4.Text = "10";
                    pictureBox1.Visible = false;
                    pictureBox2.Visible = false;
                    break;
                //Вопрос 10
                case 9:
                    label2.Text = "";
                    radioButton1.Text = "50";
                    radioButton2.Text = "10";
                    radioButton3.Text = "20";
                    radioButton4.Text = "100";
                    pictureBox1.Visible = false;
                    break;
            }
        } 
        private void button1_Click(object sender, EventArgs e)
        {

            n++;
            if (n > 19) n = 19;
            show(n);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            n--;
            if (n < 0) n = 0;
            show(n);
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            answer[n] = 1;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            answer[n] = 2;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            answer[n] = 3;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            answer[n] = 4;
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            radioButton1.Checked = false;
            radioButton2.Checked = false;
            radioButton3.Checked = false;
            radioButton4.Checked = false;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question