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

标签 c# winforms visual-studio-2010

我是 C# 的新手,试图制作一个程序,该程序本质上是一项调查,包含 30 个问题,通过选择五个单选按钮(强烈不同意、不同意...强烈同意等)之一来回答这些问题。

我设置了一个小的代码“ block ”,用于检查针对问题选中了哪个单选按钮并将值分配给数组(见下文)。

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 个问题中的前两个,并且完全按照我的需要和想法工作。

我想知道我是否可以只循环其中一个“ block ”并让它检查所有三十个问题。我搜索了又搜索,但找不到我要找的东西(我也知道我可能也没有在搜索正确的东西)。

我只是想避免在我的程序中出现三十个这样的“ block ”。我觉得这其中的三十个只会是一团糟。这可能吗?

最佳答案

首先创建一个 UserControl,它封装了单个问题的逻辑:

  • 问题文本
  • 选择的选项

一旦您有了一个问题,您就可以将任意数量的用户控件放到一个表单上,配置问题文本,然后只需循环访问一组用户控件即可获得您的答案。最好以 enumeration 的形式返回答案。 .

有多种方法可以实现这一点,例如生成控件的代码,或将选择绑定(bind)回 ViewModel 类,但用户控件是一个很好的开始。

关于c# - 在 Visual C# 中循环单选按钮检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13573955/

相关文章:

新线程中的 C# 计时器

c# - UI 对长时间的 for 循环无响应

c# - C#编译器将静态类解释为 namespace

c# - 表单在另一台计算机上看起来不同

c# - 使用Unity进行属性依赖注入(inject)的正确方法

c# - "Non-nullable event must contain a non-null value when exiting constructor"

c# - 如何以编程方式打开病毒扫描程序?

c# - 使用变量创建构造函数的快捷方式 (C# Visual Studio 2010)

c++ - _ITERATOR_DEBUG_LEVEL = 1 是什么意思?

c# - 广播或发布/订阅以更新成千上万的用户