在Visual C#中循环单选按钮检查

3

我是新手学习C#,试图制作一个程序,它基本上是一个由30个问题组成的调查表,通过选择五个单选按钮之一来回答这些问题(强烈不同意,不同意...强烈同意等)。

我已经设置了一个小的“代码块”,可以检查哪个单选按钮被选中来回答问题,并将值分配给一个数组(见下文)。

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void buttonScore_Click(object sender, EventArgs e)
    {
        this.textBoxScoreOutput.Text = " ";

        int[] score = new int[2]; // Declares the integer of score and sets it to a value of zero

        // Question 1

        if (radioButtonSD1.Checked == true) // If Strongly Disagree checked give score a value of 1
            score[0] = 1;
        else if (radioButtonD1.Checked == true) // If Disagree checked give score a value of 2
            score[0] = 2;
        else if (radioButtonNS1.Checked == true) // If Not Sure checked give score a value of 3
            score[0] = 3;
        else if (radioButtonA1.Checked == true) // If Agree checked give score a value of 4
            score[0] = 4;
        else if (radioButtonSA1.Checked == true) // If Strongly Agree is checked give score a value of 5
            score[0] = 5;

        // Question 2

        if (radioButtonSD2.Checked == true) // If Strongly Disagree checked give score a value of 1
            score[1] = 1;
        else if (radioButtonD2.Checked == true) // If Disagree checked give score a value of 2
            score[1] = 2;
        else if (radioButtonNS2.Checked == true) // If Not Sure checked give score a value of 3
            score[1] = 3;
        else if (radioButtonA2.Checked == true) // If Agree checked give score a value of 4
            score[1] = 4;
        else if (radioButtonSA2.Checked == true) // If Strongly Agree is checked give score a value of 5
            score[1] = 5;

        // Output values in array to text box

        this.textBoxScoreOutput.Text = "Array: ";

        foreach (int i in score)
        {
            this.textBoxScoreOutput.Text += "[" + i.ToString() + "] ";
        }

        int sum = score.Sum();
        this.textBoxScoreOutput.Text += "The Sum of the array is: " + sum.ToString();
    }


}
}

所以这是在检查三十个问题中的前两个,它正按照我所需要并且认为的方式工作。
我想知道是否可以循环其中的一个“块”,并检查所有三十个问题。我已经搜索了很多次,但找不到我要找的东西(我也明白我可能没有搜索正确的东西)。
我只是想避免在程序中使用三十个这样的“块”。我觉得使用三十个会很混乱。这可行吗?

单选按钮应该分组; 换句话说,一个问题应该有一个单选按钮组,该组中的每个单选按钮都有一个值。 在我看来,您应该检查单选按钮的30个(而不是30个问题的5个单独的单选按钮元素)。 如果没有看到代码和应用程序,很难确定您是否正在这样做,但这只是让它更简单的一个想法。 - Justin Helgerson
3
请说明您正在使用哪种用户界面技术。以下用户界面技术受Visual Studio支持并支持单选按钮的概念:ASP.NET Web Forms,Windows Forms,Silverlight,WPF,Windows Store应用程序。 - Richard Szalay
这些深灰色的框框是什么类型的控件? - Francis Ducharme
我在使用Windows Forms,每组有五个单选按钮在一个面板中。 - jdbodine
如果我将它们作为一组进行检查,是否仍然可以为选定的单选按钮分配特定值? - jdbodine
2个回答

2
开始创建一个UserControl,封装单个问题的逻辑:
  • 问题文本
  • 选择的选项
一旦你有了一个单独的问题,你可以将任意数量的User Control放到一个表单上,配置问题文本,然后只需要循环遍历一组User Control来获取答案。最好将答案作为枚举返回。
有许多实现方法,例如生成控件的代码,或将选择绑定回ViewModel类,但是使用User Control是一个很好的起点。

1

这是我大致的做法:

var resultList = new List<KeyValuePair<string, int>>();


foreach (var control in this.Controls)
{
    if (control is GroupBox)
    {
         GroupBox gb = (GroupBox)control;
         foreach (Control controll in gb.Controls)
         {
             if (controll is RadioButton)
             {
                RadioButton rb = new RadioButton();
                rb = (RadioButton)controll;
                //rb will allow you to access all of the RadioButton's properties and act accordingly.    
                if (rb.Checked)
                {
                    int score;
                    if (rb.Name.Contains("ButtonSD"))
                        score = 1;
                    if (rb.Name.Contains("ButtonD"))
                        score = 2;
                    //So on...
                    resultList.Add(new KeyValuePair<string, int>(gb.Name, score));

                }
             }
         }
    }
}

今天过得有点艰难,也许有人能想出更好的方法,但如果你不想重新组织整个事情,这个可能会起作用。

我觉得我以前见过这个,但我不确定这是否是我正在寻找的(尽管我也不确定我在寻找什么)。这样做是否允许我为单选按钮分配一个值,最终存储在数组“score”中?感谢您的快速回复。 - jdbodine
这很可能是你回答问题的答案,你应该仔细查看代码。如果你是新手开发者,这些都是非常基本的问题场景,而且相当容易解决。 - Master Chief
表单应该是你的表单或选项卡面板的实际名称,无论哪个控件容纳这些groupBoxes。 - Francis Ducharme
这个解决方案过于依赖于控件名称的正确性,如果你想采用这个解决方案,应该找到更好的方法来确定控件的值,比如创建一个继承自RadioButton的新类,并添加一个整数属性,然后在表单上使用这个控件。此外,提前将所有的分组框或单选按钮添加到一个列表中会极大地简化这段代码。 - benPearce
当然,但我尝试提供一个远程处理手头情况的解决方案。告诉某人(一个自称新手)放弃一切,从头开始是在提供更可靠的解决方案时有用,但在我看来还不够。 - Francis Ducharme
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接