WebzCom.com your online resource for tech of small business

Introduction to Programming
Sample Form Using C# and .Net
Sample Project - Simple Order Form (Pizza Guys)

C#, pronounced C Sharp, is a relatively new programming language from Microsoft that was designed to take full advantage of the new .Net Framework.

Below is a sample project I'm currently working on at MJC in Modesto. To test and run the code listed on this site you should download a free copy of Microsoft Visual C# Express 2005. There are four Visual Studio Express versions available for free from Microsoft, Visual Basic, C#, J# and C++.

You might want to check back from time to time if you find this information usefull as I will be trying to update it semi-regularly.

Last updated: Thursday, December 6, 2006 10:30 PM

  • Added Mike Rieben's code and screen shots [code] [screenshot] [executable]
  • Added some new code that was covered in our 10/07/2006 lecture at MJC
  • Add more comments to cover all the new code.
  • Added link to additional form code from the forms designer
  • Screen Shot

    Click to Enlarge the Screen Shot

    The Code

    A little info about what you're about to see. I created a function or module called "CalculatePizzaOrder()" which checks the status of the order form and all its elements and determines what the price of the pizza order should be then displays it on the form. At this stage of the design, this is the real core of the forms workings.

    Later I'll use this CalculatePizzaOrder module to put together an order message based on input from the form selections.

    Download a copy of Pizza_Guys.zip, the executable file.

    Click here to see the form code "Form1.Designer.cs"

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using 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, EventArgs 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 Button.

            private void btnExit_Click(object sender, EventArgs e)

            {

                Form1.ActiveForm.Close();

            }

     

            /*

             * btnSubmit_Click displays a message box that simply says Order Submitted.

             * Later I'll add more code to do something like connect to a database and

             * 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();

     

                /* 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 equivalent. 

                */

                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();

            }

     

            private void rbPizzaLarge_CheckedChanged(object sender, EventArgs e)

            {

                CalculatePizzaOrder();

            }

     

            private void checkBoxToppingSausage_CheckedChanged(object sender, EventArgs e)

            {

                CalculatePizzaOrder();

            }

     

            private void checkBoxToppingPepperoni_CheckedChanged(object sender, EventArgs e)

            {

                CalculatePizzaOrder();

            }

     

            private void checkBoxToppingOlives_CheckedChanged(object sender, EventArgs e)

            {

                CalculatePizzaOrder();

            }

     

            private void checkBoxToppingsAnchovies_CheckedChanged(object sender, EventArgs e)

            {

                CalculatePizzaOrder();

            }

     

            private void checkBoxToppingsOnions_CheckedChanged(object sender, EventArgs e)

            {

                CalculatePizzaOrder();

            }

     

            private void checkBoxToppingsExtraCheese_CheckedChanged(object sender, EventArgs 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 decide to use it

             * It kinda works but is still is a little rough around the edges but the 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 error 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.keyeventargs.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();

            }

       

        }

        }