复选框只允许一个,代码不工作

本文关键字:代码 工作 许一个 复选框 | 更新日期: 2023-09-27 18:17:46

我正在努力解决为什么我的代码不工作。这是学校的作业。我可以请求帮助。我是编程新手。我使用的是visual studio 2015。我试图得到它,所以用户必须只允许选择一个复选框。我有其他复选框在这个任务,所以使用最后一次检查将不工作。我没有得到错误,它什么也没做。谢谢!

我的复选框命名为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;
            }
        }
    }
}

复选框只允许一个,代码不工作

使用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
    }
}