在表格上选择2个单选按钮

时间:2016-04-23 15:36:51

标签: c#

我正在创建一个程序,允许用户选择2个不同的选项,1个用于宿舍,1个用于膳食计划。我正在使用单选按钮,但我一次只能选择1个按钮,我希望能够同时选择两个按钮

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int totalCharge = 0;
        totalCharge = Dorm() + MealPlan();
        Form1 TChargeForm = new Form1();
        TChargeForm.label1.Text = totalCharge.ToString("c");
        TChargeForm.ShowDialog();
    }

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

    private int Dorm()
    {
        int total = 0;

        if (radioButton1.Checked)
        {
            total += 1500;

            return total;
        }
        if (radioButton2.Checked)
        {
            total += 1600;

            return total;
        }
        if (radioButton3.Checked)
        {
            total += 1500;

            return total;
        }
        if (radioButton4.Checked)
        {
            total += 1500;

            return total;
        }
        else
        {
            return total;
        }


    }
    private int MealPlan()
    {
        int total = 0;

        if (radioButton5.Checked)
        {
            total += 600;
            return total;
        }
        if (radioButton6.Checked)
        {
            total += 1200;
            return total;
        }
        if (radioButton7.Checked)
        {
            total += 1700;
            return total;
        }
        else
        {
            return total;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

尝试为每种类型的按钮使用GroupName属性:宿舍,膳食计划

答案 1 :(得分:0)

Groupname属性允许您为单选按钮创建逻辑选择组。

将此属性添加到每个单选按钮或组框级别。

相关问题