【问题标题】:checkbox only one allowed, code not working [duplicate]复选框只允许一个,代码不起作用[重复]
【发布时间】:2016-11-14 06:55:12
【问题描述】:

我正在为为什么我的代码无法正常工作而苦苦挣扎。这是为了学校作业。我被允许寻求帮助。我是编程新手。我正在使用 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;
            }

        }
    }
}

【问题讨论】:

  • 从使用调试器开始并逐步执行您的代码,其次要与您的代码格式与您的if else blocks 更加一致,第三,查看break or return 关键字的用途以及如何使用使用它..
  • 您的代码似乎正确。你用的是什么技术?是 Windows 窗体、WPF 等。还可以查看 RadioButton,也许您正在寻找 RadioButton 的行为。
  • 我已经为这个项目添加了我的全部代码。我知道我有多个问题,但复选框是我要解决的第一个问题。我的代码没有错误。它什么也不做。我确实有一些单选按钮,但这些都是复选框。

标签: c# checkbox


【解决方案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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 2012-10-27
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多