简体   繁体   中英

C# Create a bill message box with items that were checked displayed

There's a form with three checkboxes and a button. Upon clicking on the button a messagebox is suppose to show up and display all the items that were checked on the form with price before tax and total. The price before tax and total are taken care of, but how would I display what was checked by the user on that form in the message box that will serve as a bill.

if (checkCheeseSnackBread.Checked == true)
        {
            price += 10;
            items += "Cheese Snack Bread - $10";
        }
        else
        {
            price -= 10;
        }

Just need some guidance.

First I must say that the code you've written seems to hold a logical eror. If the checkbox isn't checked then you shouldn't do anything. In your code you subtract the item-price from the total price. This way you give a discount of 10. If they don't buy it, don't add it. That's all.

So, now you know what they've bought, you can use a StringBuilder to populate the message. Quick and dirty:

StringBuilder builder = new StringBuilder();
builder.AppendLine("Ticket")
builder.AppendLine();

if (checkCheeseSnackBread.Checked) // == true is not needed
  {             
      price += 10;             
      items += "Cheese Snack Bread - $10";     
      builder.AppendLine(Cheese Snack Bread - $10);    
  }        

// Do the same for other checkboxes
// Add the totals    
Messagebox.Show(builder.ToString());

There is an other way: loop through the controls on the form, if it's a checkbox ==> add the text to the StringBuilder. This way it doesn't matter how much checkboxes there are. You just have to make sure that the text-property is used to print on the ticket (messagebox).

If you just want to display a string you generate.

Check this out:

http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM