A restaurant wants an application that calculates a table's bill. The application should display all the menu items ( shown below) in four ComboBoxes. Each ComboBox should contain a category of food offered by the restaurant (Beverage, Appetizer, Main Course and Dessert). The user can choose from one of these ComboBoxes to add an item to a table's bill. As each item is selected in the ComboBoxes, add the price of that item to the bill. The user can click the Clear Button to restore the Subtotal:, Tax:, and Total: fields to $0.00
I am having trouble with calculating the subtotal, tax, and total bill. I created a calculate button, and would like it to calculate these things when clicked. This is the code I used for that section:
You need to use the resulting value somewhere. Does no good to calculate in you are not using the result.
assuming you have text boxes on your form that need to be populated with these values then you need to add another 3 lines of code there to assign each value to the proper textbox so it will be displayed on screen.
I am using a label to show the amounts. I have a label that says Subtotal and a label next to it that is named SubAmt. That is why I set SubAmt = BevPrice + AppPrice + MainPrice + DesPrice; . I thought it would show the answer in the SubAmt label.
I also would suggest that you use lblSubAmt as your label name. As is you have a variable named SubAmt in your sub and a control named SubAmt on your form. When you get to the sub the SubAmt there will override the object with the same name that has been declared outside the sub so you will not be working with your label at this point.
If you were working with the label you would get an error as you did not specify the property of the label to set.
You have some Selected Index Changed handlers that you don't really need. Well as far as I can tell. It looks to me that the comboboxes are not dependant on one another - in other words, a dependant combobox's contents would change when as another combobox selection changed. This doesn't seem to be the case here.
If that is true then you can remove all the selected index changed handlers and just retrieve the selected indexes of the comboboxes the calculation button handler.
So in the code above, I've retrieved the selected indexes for the comboboxes and did the Cost array lookups inline. Also, you may have noticed that for naming the local variables, I've gone from PascalCase naming convention to a camelCase naming. In C#, camelCased naming is used for local and method parameter variables. So the local 'SubAmt' variable would be named 'subAmt', not 'SubAmt'.
That is an excellent point! I see your thought process here. I did try it, though, and no success. It's like no matter which code I try, when I click Calculate nothing happens.
Don't mean to be flip, but do you know how to debug your program.
Many folks new to programming don't, so it's no big deal.
What you do is click the mouse inside the calculate event handler and press F9 (you'll need to do this on a line of code for the break point to appear). Notice how you get a little red dot next to the line of code? That's the break point.
Next, start debugging by pressing F5. When the program starts up, click the 'Calculate' button. You will switch to the VS IDE and your code will stop running at the line of code where the break point is.
Now you can hover the mouse over each of the variables and look at their values. If you press 'Calculate' and the break point doesn't get hit, you've got a problem - probably because the event handler isn't hooked up to the button.
At any rate, debugging is the only way to figure out what is going wrong with the program, because letting you see what the actual values of the variables are will let you know where the problems with your code and/or logic.