首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据复选框值是选中还是notC#,将多个复选框值添加到标签

根据复选框值是选中还是notC#,将多个复选框值添加到标签
EN

Stack Overflow用户
提问于 2016-09-22 22:55:12
回答 2查看 1.2K关注 0票数 1

好的,我有8-10个复选框和单选按钮,我需要对分配给它们的双精度值求和。唯一的问题是,我只想检查一些框,而不是所有框。如果你能帮我,那就太好了。此外,我的代码上的任何指针也会有所帮助

代码语言:javascript
复制
    double SpringMix = 2.00;
    double Avocado = 1.50;
    double Beans = 2.00;
    double Cheese = 2.00;
    double Tomato = 1.50;
    double HoneyMustard = 2.00;
    double Ranch = 2.00;
    double Italian = 2.00;
    double BlueCheese = 2.00;

    double FoodCost;

我使用if语句来查看是否选中了复选框。我需要一种方法来添加他们所有取决于他们是否被选中。

代码语言:javascript
复制
public double TotalOrderCost()
    {
        if (cbSpringMix.Checked)
        {
            FoodCost + 2.00;
        }
        if (cbAvocado.Checked)
        {
            FoodCost + 1.50;
        }
        if (cbBeans.Checked)
        {
            FoodCost + 2.00;
        }
        if (cbCheese.Checked)
        {
EN

回答 2

Stack Overflow用户

发布于 2016-09-22 23:09:37

我有这个版本作为解决方案:

代码语言:javascript
复制
    private readonly Dictionary<CheckBox, double> mapping;

    public MainWindow()
    {
        InitializeComponent();
        mapping = new Dictionary<CheckBox, double>
        {
            {cbBean, 2d},
            {cbSpringMix, 2d}
            //...
        };
    }

    public double TotalOrderCost()
    {
        double foodCost = 0d;
        foreach (KeyValuePair<CheckBox, double> keyValuePair in mapping)
        {
            if (keyValuePair.Key.Checked)
            {
                foodCost += keyValuePair.Value;
            }
        }
        return foodCost;
    }

TotalOrderCost的LINQ版本:

代码语言:javascript
复制
    public double TotalOrderCost()
    {
        return mapping.Where(keyValuePair => keyValuePair.Key.Checked).Sum(keyValuePair => keyValuePair.Value);
    }

您可以添加更多的组合框,而无需修改代码的多个位置。

票数 1
EN

Stack Overflow用户

发布于 2016-09-23 00:33:49

这应该会有帮助。我编写了一个简单的控制台应用程序来演示如何做到这一点。它看起来比实际复杂得多,这只是因为我必须在其中放入额外的东西来模拟Sql或您可能使用的任何东西。我只关注食品类和配料类。它们主要是如何设置的。另外,看一下静态void main的底部,看看我是如何调用在食品类中创建的方法的。我已经更新了区域和注释,以使其更容易阅读。

代码语言:javascript
复制
class Program
{
    static void Main(string[] args)
    {

        #region SimulatedDb
        // Technically a Food object from the Database

        var myFood = new Food() { Name = "Sub", Ingredients = new List<IngredientViewModel>(), Price = 8.00 };

        // Technically Ingredients from the Database

        var cbSpringMixIn = new Ingredient() { Name = "Spring Mix", Price = 2.00 };
        var cbAvacodoIn = new Ingredient() { Name = "Avacodo", Price = 1.50 };
        var cbBeansIn = new Ingredient() { Name = "Beans", Price = 2.00 };
        #endregion

        // This would actually be in your code
        // You would probably just do a for each statement to turn all of these into 
        // Objects and add them to the list of available ingredients

        var cbSpringMix = new IngredientViewModel(cbSpringMixIn);
        var cbAvacodo = new IngredientViewModel(cbAvacodoIn);
        var cbBeans = new IngredientViewModel(cbBeansIn);

        #region SimulatedUserInterface
        Console.WriteLine("What would you like on your {0} ({1:C})", myFood.Name, myFood.Price);
        Console.WriteLine();
        Console.WriteLine("Would you like {0} ({1:C})", cbSpringMix.Name, cbSpringMix.Price);
        Console.WriteLine("yes or no");
        var answer = Console.ReadLine();
        if (answer == "yes")
        {
            cbSpringMix.Selected = true;
        }
        Console.WriteLine();
        Console.WriteLine("Would you like {0} ({1:C})", cbAvacodo.Name, cbAvacodo.Price);
        Console.WriteLine("yes or no");
        var answer1 = Console.ReadLine();
        if (answer1 == "yes")
        {
            cbAvacodo.Selected = true;
        }
        Console.WriteLine();
        Console.WriteLine("Would you like {0} ({1:C})", cbBeans.Name, cbBeans.Price);
        Console.WriteLine("yes or no");
        var answer2 = Console.ReadLine();
        if (answer2 == "yes")
        {
            cbBeans.Selected = true;
        }
        #endregion

        // This would actually be in your code
        // You would probably just do a for each statement to turn all of these into 
        // Objects and add them to the list of available ingredients

        myFood.Ingredients.Add(cbAvacodo);
        myFood.Ingredients.Add(cbBeans);
        myFood.Ingredients.Add(cbSpringMix);

        #region SimulatedUserInterfaceContinued

        Console.WriteLine("You are ready to check out! Press any key to continue...");
        var checkout = Console.ReadLine();

        Console.Write("You have ordered a {0} with ", myFood.Name);
        foreach (var ingredient in myFood.GetSelectedNames())
        {
            Console.Write(ingredient + " ");
        }
        Console.WriteLine();

        Console.WriteLine("Your Final Price is:");
        Console.WriteLine(String.Format("{0:C}", myFood.GetFullPrice()));
        Console.ReadLine();
        #endregion


    }
    public class Food
    {
        public Food()
        {

        }
        public IEnumerable<string> GetSelectedNames()
        {
            return (
                from f in this.Ingredients
                where f.Selected
                select f.Name).ToList();
        }
        public IEnumerable<double> GetSelectedValues()
        {
            return (
                from f in this.Ingredients
                where f.Selected
                select f.Price).ToList();
        }
        public double GetFullPrice()
        {
            var selectedIngredients = GetSelectedValues();
            return selectedIngredients.Sum() + this.Price;
        }
        public string Name { get; set; }
        public double Price { get; set; }

        public virtual List<IngredientViewModel> Ingredients { get; set; }

    }
    public class IngredientViewModel
    {
        public IngredientViewModel(Ingredient ingredient)
        {
            this.Name = ingredient.Name;
            this.Price = ingredient.Price;
            this.Selected = false;
        }
        public string Name { get; set; }
        public double Price { get; set; }
        public bool Selected { get; set; }

    }
    public class Ingredient
    {
        public string Name { get; set; }
        public double Price { get; set; }

    }


}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39642420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档