【问题标题】:Windows form Quiz tracking radio buttonsWindows 窗体测验跟踪单选按钮
【发布时间】:2020-09-04 19:23:40
【问题描述】:

我可以在为班级项目设置 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 Windows 窗体和一个带有 4 个单选框的组框。 radioBox1c.Checked = True 是我试图检查是否在该组中检查了该特定的人。然后它转到radiobox"XX".Checked 并做同样的事情。如果选中,它将添加到p。我的问题是我无法运行它,因为我有错误。我试过用很多 if 代替 switch,但我迷路了。显示用户无论选择什么答案都得0分,有什么办法可以解决吗?

【问题讨论】:

  • 请用返回的错误更新帖子。
  • 抱歉,我发现了问题,我在修改代码时删除了 2 个大括号。我把它们固定在垃圾场对不起。我唯一的问题是 MessageBox.Show($"你已经收集了 {p} 点数。");目前它似乎没有增加 p 的值

标签: c# visual-studio radio-button


【解决方案1】:
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();
        }
    }
}

设法解决了我自己的问题。我继续使用 if 而不是 switch,一切似乎都正常。

【讨论】:

    猜你喜欢
    • 2017-07-13
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    相关资源
    最近更新 更多