You are on page 1of 6

using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms;

namespace Pizza_Guys { public partial class Form1 : Form { //Modesto sales tax is 7.375 public const double dblTax = 7.375; DateTime CurrTime = DateTime.Now; public Form1() { InitializeComponent(); lblTaxPercentShow.Text = dblTax.ToString() + "%"; lblOrderTimeDisplay.Text = String.Format("{0:g}", CurrTime); } /* * When the credit card combo box changes check to see if cash, * check or credit card is selected. If cash or check is selected * then disable credit card #, card expiration date calendar control. */ private void comboBoxCreditCard_SelectedIndexChanged(object sender, Even tArgs e) { string strPaymentType = cBoxCreditCard.Text; if (strPaymentType == "Cash") { dateTimeCreditCardExpiration.Enabled = false; txtCreditCardNumber.Enabled = false; } else if (strPaymentType == "Check") { dateTimeCreditCardExpiration.Enabled = false; txtCreditCardNumber.Enabled = false; } else { dateTimeCreditCardExpiration.Enabled = true; txtCreditCardNumber.Enabled = true; } } //btnExit_Click closes the form when the user clicks on the btnExit Butt on. private void btnExit_Click(object sender, EventArgs e)

{ Form1.ActiveForm.Close(); } /* * btnSubmit_Click displays a message box that simply says Order Submitt ed. * Later I'll add more code to do something like connect to a database a nd * add a record using the information from the form fields. */ private void btnSubmit_Click(object sender, EventArgs e) { MessageBox.Show("Order Submitted"); } private void btnCalculate_Click(object sender, EventArgs e) { //Calculate the order CalculatePizzaOrder(); }

private void CalculatePizzaOrder() { //Calculate the order double dblPizzaPrice = 0.00; if (rbPizzaSmall.Checked) { dblPizzaPrice = 9.95; } if (rbPizzaMedium.Checked) { dblPizzaPrice = 12.95; } if (rbPizzaLarge.Checked) { dblPizzaPrice = 15.95; } if (checkBoxToppingSausage.Checked) { dblPizzaPrice = dblPizzaPrice + .50; } if (checkBoxToppingPepperoni.Checked) { dblPizzaPrice = dblPizzaPrice + .50; } if (checkBoxToppingsOlives.Checked) { dblPizzaPrice = dblPizzaPrice + .50;

} if (checkBoxToppingsAnchovies.Checked) { dblPizzaPrice = dblPizzaPrice + .50; } if (checkBoxToppingsOnions.Checked) { dblPizzaPrice = dblPizzaPrice + .50; } if (checkBoxToppingsExtraCheese.Checked) { dblPizzaPrice = dblPizzaPrice + .50; } string strQty; strQty = txtQty.Text; dblPizzaPrice = dblPizzaPrice * double.Parse(strQty); string PizzaPrice; PizzaPrice = dblPizzaPrice.ToString(); /* * * * ent. */ PizzaPrice = String.Format("{0:c}", double.Parse(PizzaPrice)); lblDisplaySubTotal.Text = PizzaPrice; lblDisplayTax.Text = String.Format("{0:c}", ((dblTax * dblPizzaPrice ) / 100)); double dblPizzaTax = (dblTax * dblPizzaPrice) / 100; double dblPizzaTotal = dblPizzaPrice + dblPizzaTax; /* * set the DisplayOrderTotal Text the value of dblPizzaTotal and * format the string to display as currency * {0:c} formats string to currency */ lblDisplayOrderTotal.Text = String.Format("{0:c}", dblPizzaTotal); } private void rbPizzaSmall_CheckedChanged(object sender, EventArgs e) { CalculatePizzaOrder(); } private void rbPizzaMedium_CheckedChanged(object sender, EventArgs e) { CalculatePizzaOrder(); } This is the easiest way to format the string for currency using String.Format("{0:c}", double.Parse(PizzaPrice)); Double.Parse (String) Converts the string representation of a number to its double-precision floating-point number equival

private void rbPizzaLarge_CheckedChanged(object sender, EventArgs e) { CalculatePizzaOrder(); } private void checkBoxToppingSausage_CheckedChanged(object sender, EventA rgs e) { CalculatePizzaOrder(); } private void checkBoxToppingPepperoni_CheckedChanged(object sender, Even tArgs e) { CalculatePizzaOrder(); } private void checkBoxToppingOlives_CheckedChanged(object sender, EventAr gs e) { CalculatePizzaOrder(); } private void checkBoxToppingsAnchovies_CheckedChanged(object sender, Eve ntArgs e) { CalculatePizzaOrder(); } private void checkBoxToppingsOnions_CheckedChanged(object sender, EventA rgs e) { CalculatePizzaOrder(); } private void checkBoxToppingsExtraCheese_CheckedChanged(object sender, E ventArgs e) { CalculatePizzaOrder(); } private void cBoxQty_SelectedIndexChanged(object sender, EventArgs e) { CalculatePizzaOrder(); } /* * Added: 11/08/2006 * This CheckEmpty Module is a work in progress so be careful if you dec ide to use it * It kinda works but is still is a little rough around the edges but th e basic * concept seems to work and shows some promise for automating the basic validation * of checking the textboxes * It is supposed to check any textbox that calls it and check to see if it is empty * then tell the user they need to enter something in the textbox */ private void CheckEmpty(object sender, EventArgs e)

{ /* * get the info on what object called the module and display an erro r message * Some of the next couple of lines I barrowed from * http://www.c-sharpcorner.com/1/win_frm1.asp * Formate ToString returns is like "Button, text: <value>" so * retrieving value from it. */ int iIndex = sender.ToString().IndexOf(":"); String sSub = sender.ToString().Substring(iIndex + 1).Trim(); if(sSub.Length == 0) MessageBox.Show("Is Empty"); } private void txtBoxName_LostFocus(object sender, EventArgs e) { MessageBox.Show(e.ToString()); CheckEmpty(sender,e); } /* * Added: 11/08/2006 * Replaced the drop down quantity comboBox with a TextBox * I did this to make my code close to what was covered * in our lecture at MJC on 10/07/2006 * * Found this explanation of Handled @ * http://msdn2.microsoft.com/en-gb/library/system.windows.forms.keyeven targs.handled.aspx * * Definition from Microsoft MSDN of Handled as it applys to a TextBox * * Handled is implemented differently by different * controls within Windows Forms. For controls like * TextBox which subclass native Win32 controls, * it is interpreted to mean that the key message * should not be passed to the underlying native control. * * If you set Handled to true on a TextBox, that control * will not pass the key press events to the underlying * Win32 text box control, but it will still display * the characters that the user typed. * * The next module uses Handled with the value set to true * if the keypress is anything other than a number. */ private void txtQty_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar < '0' e.KeyChar > '9') { e.Handled = true; } } /*

* Added: 11/08/2006 * Now that the qty in the textbox is checked to make sure it is a * number we can call the CalculatePizzaOrder() Module to recalculate * the price. */ private void txtQty_TextChanged(object sender, EventArgs e) { CalculatePizzaOrder(); } } }

You might also like