CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Jul 2011
    Posts
    7

    Exclamation button1_click help?

    The Problem:

    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:

    private void Calculate_Click(object sender, EventArgs e)
    {

    double SubAmt = BevPrice + AppPrice + MainPrice + DesPrice;
    double TaxAmt = SubAmt * 0.07;
    double TotalAmt = SubAmt + TaxAmt;
    }

    There are no errors, but I have spent days on this problem and can't figure out what else I need to do.

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: button1_click help?

    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.

    Code:
    txtSubTotal.Text=SubAmt.ToString;
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jul 2011
    Posts
    7

    Re: button1_click help?

    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.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: button1_click help?

    Quote Originally Posted by Courtneyyw View Post
    [code]
    private void Calculate_Click(object sender, EventArgs e)
    {

    double SubAmt = BevPrice + AppPrice + MainPrice + DesPrice; double TaxAmt = SubAmt * 0.07;
    double TotalAmt = SubAmt + TaxAmt;
    }
    Quote Originally Posted by Courtneyyw View Post
    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.

  5. #5
    Join Date
    Jul 2011
    Posts
    7

    Re: button1_click help?

    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).

    Do you think it could be another part of my code?

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: button1_click help?

    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.

    Label1="Something";

    is not valid

    Label1.Text="Something";

    Is valid.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: button1_click help?

    Use the ToString method when assigning the numeric to a text field.
    Code:
    Label1.Text=dblValue.ToString;
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Jul 2011
    Posts
    7

    Re: button1_click help?

    double SubAmt = BevPrice + AppPrice + MainPrice + DesPrice ;
    lblSubAmt.Text = SubAmt.ToString();

    double TaxAmt= SubAmt * 0.07;
    lblTaxAmt.Text = TaxAmt.ToString();

    double TotalAmt = SubAmt + TaxAmt;
    lblTotalAmt.Text = TotalAmt.ToString();


    Am I still completely off?

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: button1_click help?

    That looks much better! Good work! is it working?

  10. #10
    Join Date
    Jul 2011
    Posts
    7

    Re: button1_click help?

    No, it's not working. So I think my other code has something to do with it.

    I'll post it. I hope it's not completely off. Lol. I'll be a little embarrassed.

    {
    public partial class Form1 : Form
    {
    double[] BevCost = { 1.95, 1.50, 1.25, 2.95, 2.50, 1.50 };
    double[] AppCost = { 5.95, 6.95, 8.95, 8.95, 10.95, 12.95, 6.95 };
    double[] MainCost = { 15.95, 13.95, 13.95, 11.95, 19.95, 20.95, 18.95, 13.95, 14.95 };
    double[] DesCost = {5.95, 3.95, 5.95, 4.95, 5.95 };


    public Form1()
    {
    InitializeComponent();
    }
    double BevPrice = 0.0;
    double AppPrice = 0.0;
    double MainPrice = 0.0;
    double DesPrice = 0.0;

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {



    //Beverage
    switch (comboBox1.SelectedIndex)
    {
    case 0:
    BevPrice = BevCost[0];
    break;
    case 1:
    BevPrice = BevCost[1];
    break;
    case 2:
    BevPrice = BevCost[2];
    break;
    case 3:
    BevPrice = BevCost[3];
    break;
    case 4:
    BevPrice = BevCost[4];
    break;
    case 5:
    BevPrice = BevCost[5];
    break;
    }
    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {


    //Appitizer
    switch (comboBox2.SelectedIndex)
    {
    case 0:
    AppPrice = AppCost[0];
    break;
    case 1:
    AppPrice = AppCost[1];
    break;
    case 2:
    AppPrice = AppCost[2];
    break;
    case 3:
    AppPrice = AppCost[3];
    break;
    case 4:
    AppPrice = AppCost[4];
    break;
    case 5:
    AppPrice = AppCost[5];
    break;
    case 6:
    AppPrice = AppCost[6];
    break;
    }
    }

    private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
    {


    //Main Course
    switch (comboBox3.SelectedIndex)
    {
    case 0:
    MainPrice = MainCost[0];
    break;
    case 1:
    MainPrice = MainCost[1];
    break;
    case 2:
    MainPrice = MainCost[2];
    break;
    case 3:
    MainPrice = MainCost[3];
    break;
    case 4:
    MainPrice = MainCost[4];
    break;
    case 5:
    MainPrice = MainCost[5];
    break;
    case 6:
    MainPrice = MainCost[6];
    break;
    case 7:
    MainPrice = MainCost[7];
    break;
    }
    }

    private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
    {

    //Dessert
    switch (comboBox4.SelectedIndex)
    {
    case 0:
    DesPrice = DesCost[0];
    break;
    case 1:
    DesPrice = DesCost[1];
    break;
    case 2:
    DesPrice = DesCost[2];
    break;
    case 3:
    DesPrice = DesCost[3];
    break;
    case 4:
    DesPrice = DesCost[4];
    break;
    }

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

    double SubAmt = BevPrice + AppPrice + MainPrice + DesPrice ;
    lblSubAmt.Text = SubAmt.ToString();

    double TaxAmt= SubAmt * 0.07;
    lblTaxAmt.Text = TaxAmt.ToString();

    double TotalAmt = SubAmt + TaxAmt;
    lblTotalAmt.Text = TotalAmt.ToString();




    }
    private void Clear_Click(object sender, EventArgs e)
    {
    Clear.ResetText();
    }
    }
    }

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Post Re: button1_click help?

    Please use code tags.

    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.

    Code:
    private void Calculate_Click(object sender, EventArgs e)
    {
    
      double subAmt = BevCost[ comboBox1.SelectedIndex ]
                              + AppCost[ comboBox2.SelectedIndex ]
                              + MainCost[ comboBox3.SelectedIndex ]
                              + DesCost[ comboBox4.SelectedIndex ];
    
      lblSubAmt.Text = subAmt.ToString();
    
      double taxAmt= subAmt * 0.07;
      lblTaxAmt.Text = taxAmt.ToString();
    
      double totalAmt = subAmt + taxAmt;
      lblTotalAmt.Text = totalAmt.ToString();
    }
    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'.

  12. #12
    Join Date
    Jul 2011
    Posts
    7

    Re: button1_click help?

    Arjay,

    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.

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: button1_click help?

    Quote Originally Posted by Courtneyyw View Post
    Arjay,

    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.

  14. #14
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: button1_click help?

    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)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: button1_click help?

    And there is F10 and F11 too.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured