Windows窗体测验跟踪单选按钮

问题描述 投票:0回答:1

我可以为类项目设置Windows窗体测验时使用一些帮助。

namespace WindowsFormsApp7
{
    public partial class Form1 : Form
    {

        //I'm using p to track the amount of points the user collects through the quiz.

        int p = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        #region Submit Button & Point Calculation
        private void button1_Click(object sender, EventArgs e)
        {
            //Calculating the amount of point the user collected after submitting his answers.
            switch (p)
            {
                case 1:
                    radioButton1c.Checked = true;
                    p++;
                    break;
                case 2:
                    radioButton2a.Checked = true;
                    p++;
                    break;
                case 3:
                    radioButton3b.Checked = true;
                    p++;
                    break;
                case 4:
                    radioButton4c.Checked = true;
                    p++;
                    break;
                case 5:
                    radioButton5a.Checked = true;
                    p++;
                    break;
                case 6:
                    radioButton6a.Checked = true;
                    p++;
                    break;
                case 7:
                    radioButton7c.Checked = true;
                    p++;
                    break;
                case 8:
                    radioButton8d.Checked = true;
                    p++;
                    break;
                case 9:
                    radioButton9a.Checked = true;
                    p++;
                    break;
                case 10:
                    radioButton10b.Checked = true;
                    p++;
                    break;
                }
            MessageBox.Show($"You have collected {p} amount of points.");
        }
    }
}

到目前为止,这是我为一个班级项目准备的。我基本上有一个带有标签的Visual Studio窗口表单和一个带有4个单选框的分组框。 radioBox1c.Checked = True是我试图检查是否在该组中检查了那个特定的那个。然后移至radiobox"XX"。选中并执行相同操作。如果选中,它将添加到p。我的问题是我无法运行,因为我有错误。我尝试使用很多if代替开关,但我迷路了。它表明无论选择什么答案,用户都将获得0分,有什么办法可以解决?

c# visual-studio radio-button
1个回答
0
投票
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp7
{
    public partial class Form1 : Form
    {

        //I'm using p to track the amount of points the user collects through the quiz.

        int p=0;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Calculating the amount of point the user collected after submitting his answers.
            if (radioButton1c.Checked == true)
            {
            p++;
            }
            if (radioButton2a.Checked == true)
            {
                p++;
            }
            if (radioButton3b.Checked == true)
            {
                p++;
            }
            if (radioButton4c.Checked == true)
            {
                p++;
            }
            if (radioButton5a.Checked == true)
            {
                p++;
            }
            if (radioButton6a.Checked == true)
            {
                p++;
            }
            if (radioButton7c.Checked == true)
            {
                p++;
            }
            if (radioButton8d.Checked == true)
            {
                p++;
            }
            if (radioButton9a.Checked == true)
            {
                p++;
            }
            if (radioButton10b.Checked == true)
            {
                p++;
            }


            MessageBox.Show($"You have earned {p} point(s).");

            Application.Exit();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.