复选框只允许一个,代码无效

时间:2016-11-13 17:08:40

标签: c# checkbox

我正在努力解决为什么我的代码无效。这是为了学校作业。我被允许寻求帮助。我是编程新手。我正在使用visual studio 2015.我试图获得它,因此必须只允许用户选择一个复选框。我在此分配中有其他复选框,因此使用上次检查将无效。我没有得到错误,它什么也没做。谢谢!

我的checkBox被命名为checkBox1,checkBox2,...... 5

我目前的全部代码是:

    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 Chapter6Homework
{
    public partial class IceCreamOrder : Form
    {
        public IceCreamOrder()
        {
            InitializeComponent();
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            // Clear flavors by automatically selecting default button on Clear button click
            rdbDefault.Checked = true;

            // Clear toppings
            checkBox_CookieDough.CheckState = CheckState.Unchecked;
            checkBox_ChocolateSyrup.CheckState = CheckState.Unchecked;
            checkBox_Marshmallows.CheckState = CheckState.Unchecked;
            checkBox_OreoPieces.CheckState = CheckState.Unchecked;
            checkbox_Sprinkles.CheckState = CheckState.Unchecked;
            checkbox_Walnuts.CheckState = CheckState.Unchecked;

            // Clear List Box
            lstDisplay.Items.Clear();

            // Clear scoops 
            checkBox1.CheckState = CheckState.Unchecked;
            checkBox2.CheckState = CheckState.Unchecked;
            checkBox3.CheckState = CheckState.Unchecked;
            checkBox4.CheckState = CheckState.Unchecked;
            checkBox5.CheckState = CheckState.Unchecked;
        }

        private void btn_CalculateCost_Click(object sender, EventArgs e)
        {
            // Verify user selected a flavor
            if (rdbDefault.Checked == true)
            {
                MessageBox.Show("Please select a flavor");
                return;
            }

            // Verify user seleted # of scoops
            if (checkBox1.CheckState == CheckState.Unchecked &&
                    checkBox2.CheckState == CheckState.Unchecked &&
                    checkBox3.CheckState == CheckState.Unchecked &&
                    checkBox4.CheckState == CheckState.Unchecked &&
                    checkBox5.CheckState == CheckState.Unchecked)
            {
                MessageBox.Show("You must select a number of scoops. 1 is a must but 5 is recommended!");
                return;
            }

            //Verify user got the toppings they wanted if any
            if (checkBox_ChocolateSyrup.CheckState == CheckState.Unchecked &&
                checkBox_CookieDough.CheckState == CheckState.Unchecked &&
                checkBox_Marshmallows.CheckState == CheckState.Unchecked &&
                checkBox_OreoPieces.CheckState == CheckState.Unchecked &&
                checkbox_Sprinkles.CheckState == CheckState.Unchecked &&
                checkbox_Walnuts.CheckState == CheckState.Unchecked)
            {
                DialogResult dr = MessageBox.Show("Are you sure you don't want toppings?",
                    "help", MessageBoxButtons.YesNo);
                switch (dr)
                {
                    case DialogResult.Yes: break;
                    case DialogResult.No: return;
                }
            }

            // Declare Variables and constants
            double flavorCost = FlavorCost();
            double toppingCost = ToppingCost();
            double scoops = Scoops() * flavorCost;
            double subTotal = (flavorCost + toppingCost + scoops);
            double salesTax = subTotal * .08;
            double total = subTotal + salesTax;

            // Display total price of order
            lstDisplay.Items.Clear();
            lstDisplay.Items.Add("Total:  " + total.ToString("C2"));

            // Display total sales tax
            lstDisplay.Items.Add("");
            lstDisplay.Items.Add("Sales Tax:  " + salesTax.ToString("C2"));

            // Display Flavor Cost
            lstDisplay.Items.Add("Flavor:        " + flavorCost.ToString("C2"));

            // Display Scoops Cost
            lstDisplay.Items.Add("Scoops:      " + scoops.ToString("C2"));

            // Display Toppings
            lstDisplay.Items.Add("Toppings:   " + toppingCost.ToString("C2"));
        }
        // Get flavor cost
        Double FlavorCost()
        {
            if ((radioButton_Chocolate.Checked == true) || (radioButton_Strawberry.Checked == true))
                return 1.5F;
            else if (radioButton_Vanilla.Checked == true)
                return 1.25F;
            else
                return 0;
        }

        // Get num of scoops
        Double Scoops()
        {
            if (checkBox1.Checked == true)
                return 1;
            else if (checkBox2.Checked == true)
                return 2;
            else if (checkBox3.Checked == true)
                return 3;
            else if (checkBox4.Checked == true)
                return 4;
            else if (checkBox5.Checked == true)
                return 5;
            else
                return 0;
        }

        // Get Toppings
        Double ToppingCost()
        {
            if ((checkBox_ChocolateSyrup.Checked == true) ||
                (checkBox_Marshmallows.Checked == true) ||
                (checkbox_Sprinkles.Checked == true))
                return .25F;
            else if ((checkBox_OreoPieces.Checked == true) ||
                     (checkBox_CookieDough.Checked == true) ||
                     (checkbox_Walnuts.Checked == true))
                return .50F;
            else
                return 0;
        }

        private void IceCreamOrder_Load_1(object sender, EventArgs e)
        {
            //Set Default to true on load
            rdbDefault.Checked = true;
        }

        internal class Sub
        {
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            int numberChecked = 0;
            CheckBox[] array = new
            CheckBox[] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5 };
            for (int i = 0; i < array.Length; i++)
            {
                if
                (array[i].Checked)
                    numberChecked++;
                else if
                (numberChecked > 1)
                    MessageBox.Show("You have checked "
                 + numberChecked.ToString() + " checkBoxes. Only one is allowed.");
                else
                    return;
            }

        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用RadioButton进行分组。

使您的解决方案有效:(发件人是选中的复选框)

private void checkBox1_Checked(object sender, EventArgs e)
{
var array = new CheckBox[] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5 };
foreach(var checkbox in array)
{
    if(checkbox != sender){
        checkbox.IsChecked = false
    }
}