Как рассчитать выбранные пары ключевых значений combobox?
Все работает отлично, но я не могу вывести правильные вычисления (subtotal, tax, total) для выбранных пар KeyValuePairs в разных comboBox. Спасибо!
Что я уже пробовал:
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); } } }
[no name]
https://www.codeproject.com/Answers/1182257/How-to-remove-duplicate-keys-in-different-combobox#answer1
OfficialSub0
Однако отладчик не показывает никаких синтаксических ошибок. Я не уверен, что правильная формула, очевидно, та, которую я использую, не дает желаемых результатов, поэтому я немного запутался?
Richard MacCutchan
Где находится та часть, которая делает расчет, и что не так с результатами.
Кстати, использование двойных типов для финансовых расчетов неверно, вы получите ошибки округления.