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 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.
OK, you say you have a label named SubAmt, but, you are creating a variable SubAmt as well - look at the above highlighted line. Your answer gets stored inside that variable.
Also, to set the Text property you need to set it as DM pointed out :
Code:
SubAmt.Text = Whatever;
As a Side note, because you are new to C# in general. Having two objects named the same is bad practise. With Form controls, you must indicate what type of control it is, for better understanding.
You could use Camel Case names or Pascal based names. A good example for your label would be :
Code:
lblSubAmt
There, just by looking at it I can see that it is a label.
Thank you HanneS! I see exactly what you are saying about creating a new variable. I did that because when I typed SubAmt.Text = ....; , I always got an error message.
Error 1 Cannot implicitly convert type 'double' to 'string' OR 'double' to 'System.Windows.Forms.Label'
When I added double, the message went away, but I didn't even think about it creating a new variable. So I'm back to where I started (with the error messages).
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.
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.
To add to that, you can START and RESUME EXECUTION, but only ONE LINE AT A TIME, by pressing the F8 Key after a breakpoint, or to start from the beginning (and go thru the WHOLE program step by step)
Bookmarks