如何计算组合框的选定键值对? [英] How do I calculate selected keyvaluepairs of combobox's?

查看:69
本文介绍了如何计算组合框的选定键值对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切都运行得很好但我似乎无法为不同的comboBox中的选定KeyValuePairs输出正确的计算(小计,税,​​总计)。谢谢!



我尝试过:



Everything works perfectly fine but I can't seem to output the correct calculations (subtotal, tax, total) for the selected KeyValuePairs in the different comboBox's. Thanks!

What I have tried:

namespace BillCalculator
{
    public partial class Form1 : Form
    {
        double total = 0;
        double subtotal = 0;
        double tax = 0;

        public Form1()
        {
            InitializeComponent();

            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Create a List to store our KeyValuePairs
            List<KeyValuePair<string, double>> data = new List<KeyValuePair<string, double>>();

            //**BEVERAGE**           
            data.Add(new KeyValuePair<string, double>("Select Item", 0.00));
            data.Add(new KeyValuePair<string, double>("Soda", 1.95));
            data.Add(new KeyValuePair<string, double>("Tea", 1.50));
            data.Add(new KeyValuePair<string, double>("Coffee", 1.25));
            data.Add(new KeyValuePair<string, double>("Mineral Water", 2.95));
            data.Add(new KeyValuePair<string, double>("Juice", 2.50));
            data.Add(new KeyValuePair<string, double>("Milk", 1.50));

            // Clear the combobox
            beverageBox.DataSource = null ;
            beverageBox.Items.Clear();

            // Bind the combobox
            beverageBox.DataSource = new BindingSource(data, null);
            beverageBox.DisplayMember = "Key";
            beverageBox.ValueMember = "Value";


            //**APPETIZER**

            
            {
                List<KeyValuePair<string, double>> data1 = new List<KeyValuePair<string, double>>();

                appetizerBox.BindingContext = new BindingContext();

                data1.Add(new KeyValuePair<string, double>("Select Item", 0.00));
                data1.Add(new KeyValuePair<string, double>("Buffalo Wings", 5.95));
                data1.Add(new KeyValuePair<string, double>("Buffalo Fingers", 6.95));
                data1.Add(new KeyValuePair<string, double>("Potato Skins", 8.95));
                data1.Add(new KeyValuePair<string, double>("Nachos", 8.95));
                data1.Add(new KeyValuePair<string, double>("Mushroom Caps", 10.95));
                data1.Add(new KeyValuePair<string, double>("Shrimp Cocktail", 12.95));
                data1.Add(new KeyValuePair<string, double>("Chips and Sala", 6.96));

                // Clear the combobox
                appetizerBox.DataSource = null;
                appetizerBox.Items.Clear();

                // Bind the combobox
                appetizerBox.DataSource = new BindingSource(data1, null);
                appetizerBox.DisplayMember = "Key";
                appetizerBox.ValueMember = "Value";
            }

            //**MAIN COURSE**
            {
                List<KeyValuePair<string, double>> data2 = new List<KeyValuePair<string, double>>();

                mainCBox.BindingContext = new BindingContext();

                data2.Add(new KeyValuePair<string, double>("Select Item", 0.00));
                data2.Add(new KeyValuePair<string, double>("Chicken Alfredo", 13.95));
                data2.Add(new KeyValuePair<string, double>("Chicken Picatta", 13.95));
                data2.Add(new KeyValuePair<string, double>("Turkey Club", 11.95));
                data2.Add(new KeyValuePair<string, double>("Lobster Pie", 19.95));
                data2.Add(new KeyValuePair<string, double>("Prime Rib", 20.95));
                data2.Add(new KeyValuePair<string, double>("Shrimp Scampi", 18.95));
                data2.Add(new KeyValuePair<string, double>("Turkey Dinner", 13.96));
                data2.Add(new KeyValuePair<string, double>("Stuffed Chicken", 14.96));
                data2.Add(new KeyValuePair<string, double>("Seafood Alfredo", 15.96));
                
                // Clear the combobox
                mainCBox.DataSource = null;
                mainCBox.Items.Clear();

                // Bind the combobox
                mainCBox.DataSource = new BindingSource(data2, null);
                mainCBox.DisplayMember = "Key";
                mainCBox.ValueMember = "Value";
            }

            //**DESSERT**
            {
                List<KeyValuePair<string, double>> data3 = new List<KeyValuePair<string, double>>();

                dessertBox.BindingContext = new BindingContext();

                data3.Add(new KeyValuePair<string, double>("Select Item", 0.00));
                data3.Add(new KeyValuePair<string, double>("Apple Pie", 5.95));
                data3.Add(new KeyValuePair<string, double>("Sundae", 3.95));
                data3.Add(new KeyValuePair<string, double>("Carrot Cake", 5.95));
                data3.Add(new KeyValuePair<string, double>("Mud Pie", 4.95));
                data3.Add(new KeyValuePair<string, double>("Apple Crisp", 5.95));
                
                // Clear the combobox
                dessertBox.DataSource = null;
                dessertBox.Items.Clear();

                // Bind the combobox
                dessertBox.DataSource = new BindingSource(data3, null);
                dessertBox.DisplayMember = "Key";
                dessertBox.ValueMember = "Value";
            }
        }
        private void Appetizer_Click(object sender, EventArgs e)
        {

        }
 
  
        private void beverageBox_SelectedIndexChanged(object sender, EventArgs e)
        {
          // Get the selected item in the combobox
    KeyValuePair<string, double> selectedPair = (KeyValuePair<string, double>)beverageBox.SelectedItem;

            // Show selected information on screen
            lblSelectedKey.Text = selectedPair.ToString();
            total = selectedPair.Value;
        }

        private void appetizerBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get the selected item in the combobox
            KeyValuePair<string, double> selectedPair = (KeyValuePair<string, double>)appetizerBox.SelectedItem;

            // Show selected information on screen
            lblSelectedKey2.Text = selectedPair.ToString();
            total = selectedPair.Value;
        }

        private void mainCBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get the selected item in the combobox
            KeyValuePair<string, double> selectedPair = (KeyValuePair<string, double>)mainCBox.SelectedItem;

            // Show selected information on screen
            lblSelectedKey3.Text = selectedPair.ToString();
            total = selectedPair.Value;
        }

        private void dessertBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get the selected item in the combobox
            KeyValuePair<string, double> selectedPair = (KeyValuePair<string, double>)dessertBox.SelectedItem;

            // Show selected information on screen
            lblSelectedKey4.Text = selectedPair.ToString();
            total = selectedPair.Value;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
    

            if (beverageBox.SelectedIndex == 0)
            {
                subtotal = total;
            }
           
            if (appetizerBox.SelectedIndex == 0)
            {
                subtotal = total;
            }

            if (mainCBox.SelectedIndex == 0)
            {
                subtotal = total;                 
            }

            if (dessertBox.SelectedIndex == 0)
            {
                subtotal = total;
            }
           
            textBox1.Text = Convert.ToString(subtotal);
            tax = subtotal * 0.2;
            textBox2.Text = Convert.ToString(tax);
            total = tax + subtotal;
            textBox3.Text = Convert.ToString(total);
        }
    }
 }

推荐答案

参考你的评论
Quote:

虽然调试器没有显示语法错误

Debugger is showing no syntax errors though

尝试正确使用您的术语。如果您有语法错误,则无法进行调试。 编译器将报告语法(和其他)错误。编译完程序后,就可以运行它 - 在运行时完成调试,并在数据或逻辑出现问题时报告异常。调试很快就向我展示了代码的问题。本文将帮助您掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



你的代码有很多问题。



1.作为@ Richard-MacCutchan表示你不应该使用 double 进行财务计算 - 参见十进制与双重差异? [ ^ ] - 使用十进制您需要将硬编码值转换为十进制或者使用后缀 m 来表示小数,即

Try to get your terminology right. You cannot debug if you have syntax errors. The compiler will report syntax (and other) errors. Once you have compiled your program you can run it - debugging is done at runtime and can report exceptions when there are problems with the data or logic. Debugging is what quickly showed me the problem with your code. This article will help you Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

You have a number of issues with your code.

1. As @Richard-MacCutchan stated you should not use double for financial calculations - see Decimal vs. Double - difference?[^] - use decimalYou will need to either cast the hard-coded values to decimal OR use the suffix m to indicate decimal i.e.

data.Add(new KeyValuePair<string, decimal>("Select Item", 0.00m));

2。每次从其中一个组合框中选择项目时,您替换 总计的值。这不仅仅是添加到总

2. You are replacing the value of total each time you select an item from one of the ComboBoxes. It is not just a case of adding to total

total += selectedPair.Value;

的情况,因为您之前可能已经从该列表中选择了一个项目。换句话说,您不应该使用 SelectedIndexChanged 事件来累加总值。按钮单击事件是进行添加的正确位置。您可以忽略我的其余解决方案,因为这一点可以解决您的问题,但我建议您查看所有问题。



3.真的很长查看按钮上的代码片段单击

because you may have previously selected an item from that list. In other words you should not be using the SelectedIndexChanged event to add up the total value. The button click event is the correct place to do the adding up. You can probably ignore the rest of my solution as this point will solve your problem, but I urge you to look at all of it.

3. Have a really long look at the code snippet from the button click

if (beverageBox.SelectedIndex == 0)
{
    subtotal = total;
}

if (appetizerBox.SelectedIndex == 0)
{
    subtotal = total;
}

if (mainCBox.SelectedIndex == 0)
{
    subtotal = total;
}

if (dessertBox.SelectedIndex == 0)
{
    subtotal = total;
}

您只需将小计设置为总计如果组合框选择的项目是第一个项目选择项目,则为值。因此,当您选择所有内容时,小计实际为0.



4.行

You are only setting subtotal to the total value if the combobox selected item is the very first item "Select Item". So by the time you select everything subtotal is actually 0.

4. The line

textBox1.Text = Convert.ToString(subtotal);

非常可怕。

textBox1.Text = subtotal.ToString();

更加整洁,您可以在调用ToString()时定义要使用的文化。例如,一些欧洲国家使用逗号而不是句点来表示小数点 - 这将由文化设置决定。所以按钮点击事件变得更加整洁,目前看起来像这样:

is much tidier plus you can define the culture you want to use in the call to ToString(). For example, some European countries use a comma instead of a period to indicate a decimal point - this would be determined by the culture setting. So the button click event is getting tidier and currently looks like this:

private void button1_Click(object sender, EventArgs e)
{
    if (beverageBox.SelectedIndex != -1)
        subtotal += ((KeyValuePair<string, decimal>)beverageBox.SelectedItem).Value; 
    if (appetizerBox.SelectedIndex != -1)
        subtotal += ((KeyValuePair<string, decimal>)appetizerBox.SelectedItem).Value; 
    if (mainCBox.SelectedIndex != -1)
        subtotal += ((KeyValuePair<string, decimal>)mainCBox.SelectedItem).Value; 
    if (dessertBox.SelectedIndex != -1)
        subtotal += ((KeyValuePair<string, decimal>)dessertBox.SelectedItem).Value;

    textBox1.Text = subtotal.ToString(CultureInfo.CurrentCulture);
    tax = subtotal * (decimal) 0.2;
    textBox2.Text = tax.ToString(CultureInfo.CurrentCulture);
    total = tax + subtotal;
    textBox3.Text = total.ToString(CultureInfo.CurrentCulture);
}





5.您声明变量总计,<表单级别的code>小计和 tax (全局变量)。这对于总计来说是可以理解的,因为你在整个地方都指的是它,但对于其他两个则没有必要。通常认为将变量尽可能接近它们的使用声明是好的做法,要么紧接在使用变量的第一行之前,要么在使用它们的方法的最开始时。



6.有很多我喜欢称之为几乎重复的代码。每个 SelectedIndexChanged 事件非常非常相似 - 它只是每种情况下控件的名称不同。我可能会将所有常见的东西放在一个方法中并调用它。



这是我修改后的代码版本。注意我已经将填充ComboBoxes的代码移动到一个单独的方法中 - 将信息显示与数据检索分开 - 这样可以更容易地移动到n层表单。



5. You declared the variables total, subtotal and tax at the form level ("global variables"). This was understandable for total as you were referring to it all over the place, but for the other two it was not necessary. It is usually considered good practice to declare variables as close as possible to their use, either immediately before the first line to use a variable, OR at the very beginning of the method where they are used.

6. There is quite a lot of what I like to call "almost duplication" of code. Each of the SelectedIndexChanged events are very, very similar - it is just the name of the control that is different in each case. I would probably put all of the common stuff in a single method and call that.

Here is my revised version of your code. Note I've moved the code that populates the ComboBoxes into a separate method - to separate the display of information from the retrieval of data - it will make it easier to move to a n-tier form.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private static List<KeyValuePair<string, decimal>> GetBeverages()
    {
        return new List<KeyValuePair<string, decimal>>
        {
            new KeyValuePair<string, decimal>("Select Item", 0.00m),
            new KeyValuePair<string, decimal>("Soda", 1.95m),
            new KeyValuePair<string, decimal>("Tea", 1.50m),
            new KeyValuePair<string, decimal>("Coffee", 1.25m),
            new KeyValuePair<string, decimal>("Mineral Water", 2.95m),
            new KeyValuePair<string, decimal>("Juice", 2.50m),
            new KeyValuePair<string, decimal>("Milk", 1.50m)
        };
    }
    private static List<KeyValuePair<string, decimal>> GetAppetizers()
    {
        return new List<KeyValuePair<string, decimal>>
        {
            new KeyValuePair<string, decimal>("Select Item", 0.00m),
            new KeyValuePair<string, decimal>("Buffalo Wings", 5.95m),
            new KeyValuePair<string, decimal>("Buffalo Fingers", 6.95m),
            new KeyValuePair<string, decimal>("Potato Skins", 8.95m),
            new KeyValuePair<string, decimal>("Nachos", 8.95m),
            new KeyValuePair<string, decimal>("Mushroom Caps", 10.95m),
            new KeyValuePair<string, decimal>("Shrimp Cocktail", 12.95m),
            new KeyValuePair<string, decimal>("Chips and Sala", 6.96m)
        };
    }
    private static List<KeyValuePair<string, decimal>> GetMains()
    {
        return new List<KeyValuePair<string, decimal>>
        {
            new KeyValuePair<string, decimal>("Select Item", 0.00m),
            new KeyValuePair<string, decimal>("Chicken Alfredo", 13.95m),
            new KeyValuePair<string, decimal>("Chicken Picatta", 13.95m),
            new KeyValuePair<string, decimal>("Turkey Club", 11.95m),
            new KeyValuePair<string, decimal>("Lobster Pie", 19.95m),
            new KeyValuePair<string, decimal>("Prime Rib", 20.95m),
            new KeyValuePair<string, decimal>("Shrimp Scampi", 18.95m),
            new KeyValuePair<string, decimal>("Turkey Dinner", 13.96m),
            new KeyValuePair<string, decimal>("Stuffed Chicken", 14.96m),
            new KeyValuePair<string, decimal>("Seafood Alfredo", 15.96m)
        };
    }

    private static List<KeyValuePair<string, decimal>> GetDesserts()
    {
        return new List<KeyValuePair<string, decimal>>
        {
            new KeyValuePair<string, decimal>("Select Item", 0.00m),
            new KeyValuePair<string, decimal>("Apple Pie", 5.95m),
            new KeyValuePair<string, decimal>("Sundae", 3.95m),
            new KeyValuePair<string, decimal>("Carrot Cake", 5.95m),
            new KeyValuePair<string, decimal>("Mud Pie", 4.95m),
            new KeyValuePair<string, decimal>("Apple Crisp", 5.95m)
        };
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        //**BEVERAGE**           
        beverageBox.DataSource = null;
        beverageBox.Items.Clear();
        beverageBox.DataSource = new BindingSource(GetBeverages(), null);
        beverageBox.DisplayMember = "Key";
        beverageBox.ValueMember = "Value";

        //**APPETIZER**
        appetizerBox.DataSource = null;
        appetizerBox.Items.Clear();
        appetizerBox.DataSource = new BindingSource(GetAppetizers(), null);
        appetizerBox.DisplayMember = "Key";
        appetizerBox.ValueMember = "Value";

        //**MAIN COURSE**
        mainCBox.DataSource = null;
        mainCBox.Items.Clear();
        mainCBox.DataSource = new BindingSource(GetMains(), null);
        mainCBox.DisplayMember = "Key";
        mainCBox.ValueMember = "Value";

        //**DESSERT**
        dessertBox.DataSource = null;
        dessertBox.Items.Clear();
        dessertBox.DataSource = new BindingSource(GetDesserts(), null);
        dessertBox.DisplayMember = "Key";
        dessertBox.ValueMember = "Value";
    }

    private static void ShowSelected(object item, Control label)
    {
        // Check that something has actually been selected
        if (item == null) return;

        // Get the selected item in the combobox
        var selectedPair = (KeyValuePair<string, decimal>)item;

        // Show selected information on screen
        label.Text = selectedPair.ToString();
    }

    private void beverageBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowSelected(beverageBox.SelectedItem, lblSelectedKey);
    }

    private void appetizerBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowSelected(appetizerBox.SelectedItem, lblSelectedKey2);
    }

    private void mainCBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowSelected(mainCBox.SelectedItem, lblSelectedKey3);
    }

    private void dessertBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowSelected(dessertBox.SelectedItem, lblSelectedKey4);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var subtotal = AddToTotal(beverageBox);
        subtotal += AddToTotal(appetizerBox);
        subtotal += AddToTotal(mainCBox);
        subtotal += AddToTotal(dessertBox);

        textBox1.Text = subtotal.ToString(CultureInfo.CurrentCulture);
        var tax = subtotal * (decimal) 0.2;
        textBox2.Text = (subtotal * (decimal)0.2).ToString(CultureInfo.CurrentCulture);
        textBox3.Text = (tax + subtotal).ToString(CultureInfo.CurrentCulture);
    }

    private decimal AddToTotal(ComboBox cb)
    {
        //Check 0 if nothign actually selected
        return cb.SelectedIndex != -1 ? ((KeyValuePair<string, decimal>) cb.SelectedItem).Value : 0;
    }
}


这篇关于如何计算组合框的选定键值对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆