C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 307,172 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,571 people online right now. Registration is fast and FREE... Join Now!




Combo Box

 

Combo Box

Leizifei

15 Jul, 2009 - 04:46 PM
Post #1

New D.I.C Head
*

Joined: 3 Jun, 2009
Posts: 2

Hello iam having trouble to find out how to add the prices that I stored in the combo box

and also adding an double variable tax to the total for each item

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

namespace Restaurant
{
    public partial class Form1 : Form
    {
        class userInput
        {

            private double _value;
            private string _name;

            public double Value
            {
                get { return _value; }
                set { _value = value; }
            }
            public string Name
            {

                get { return _name; }
                set { _name = value; }

            }
            public userInput(string name, double value)
            {

                _name = name;

                _value = value;

            }
            public override string ToString()
            {
                return _name;
            }
        }
        public Form1()
        {
            InitializeComponent();

            cbBeverage.Items.Add(new userInput("Soda", 1.95));
            cbBeverage.Items.Add(new userInput("Tea", 1.50));
            cbBeverage.Items.Add(new userInput("Cofee", 1.25));
            cbBeverage.Items.Add(new userInput("Mineral Water", 2.95));
            cbBeverage.Items.Add(new userInput("Juice", 2.50));
            cbBeverage.Items.Add(new userInput("Milk", 1.50));

            cbAppe.Items.Add(new userInput("Buffalo Wings", 5.95));
            cbAppe.Items.Add(new userInput("Buffalo Fingers", 6.95));
            cbAppe.Items.Add(new userInput("Potatoe Skins", 8.95));
            cbAppe.Items.Add(new userInput("Nachos", 8.95));
            cbAppe.Items.Add(new userInput("Mushroom Caps", 10.95));
            cbAppe.Items.Add(new userInput("Shrimp CockTail", 12.95));
            cbAppe.Items.Add(new userInput("Chips And Salsa", 6.95));

            cbMain.Items.Add(new userInput("Seafood Alfredo", 15.95));
            cbMain.Items.Add(new userInput("Chicken Alfredo", 13.95));
            cbMain.Items.Add(new userInput("Chicken Picatta", 13.95));
            cbMain.Items.Add(new userInput("Turkey Club", 11.95));
            cbMain.Items.Add(new userInput("Lobster Pie", 19.95));
            cbMain.Items.Add(new userInput("Prime Rib", 20.95));
            cbMain.Items.Add(new userInput("Shrimp Scampi", 18.95));
            cbMain.Items.Add(new userInput("Turkey Dinner", 13.95));
            cbMain.Items.Add(new userInput("Stuffed chicken", 14.95));

            cbDessert.Items.Add(new userInput("Apple Pie", 5.95));
            cbDessert.Items.Add(new userInput("Sundae", 3.95));
            cbDessert.Items.Add(new userInput("Carrot cake", 5.95));
            cbDessert.Items.Add(new userInput("Mudd Pie", 4.95));
            cbDessert.Items.Add(new userInput("Apple crisp", 5.95));

        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }
        private void cbBeverage_SelectedIndexChanged(object sender, EventArgs e)
        {
            userInput selectedData = (userInput)cbBeverage.SelectedItem;
            lblBeverage.Text = selectedData.Name + ":" + selectedData.Value;
        }

        private void cbAppe_SelectedIndexChanged(object sender, EventArgs e)
        {
            userInput selectedData = (userInput)cbAppe.SelectedItem;
            lblAppe.Text = selectedData.Name + ":" + selectedData.Value;


        }

        private void cbMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            userInput selectedData = (userInput)cbMain.SelectedItem;
            lblMain.Text = selectedData.Name + ":" + selectedData.Value;

        }

        private void cbDessert_SelectedIndexChanged(object sender, EventArgs e)
        {
            userInput selectedData = (userInput)cbDessert.SelectedItem;
            lblDessert.Text = selectedData.Name + ":" + selectedData.Value;

        }
    }
}


User is offlineProfile CardPM
+Quote Post

 
Reply to this topicStart new topic
Replies(1 - 1)

SixOfEleven

RE: Combo Box

15 Jul, 2009 - 08:23 PM
Post #2

lives.ToCode();
Group Icon

Joined: 18 Oct, 2008
Posts: 3,075



Thanked: 171 times
Dream Kudos: 825
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
This is one approach to solving your problem. First you will need to add some variables to your code, to keep track of the cost of your appetiser, beverage, main course and dessert. It would be a good idea to have a variable for the tax and the total as well. You should also define a const double for the tax rate. It is important to set these values to 0.0, except the tax rate which would be your tax rate.

csharp

const double taxRate = 0.08;
double beverage = 0.0;
double appe = 0.0;
double main = 0.0;
double dessert = 0.0;
double tax = 0.0;
double total = 0.0;


Now, in each of your methods that handle when the selected index is changed you would want to get the value of item and assign it to the appropriate vairable. Then, call a method that I will show you in a moment. Here is a sample method to do the above, for the beverage:

csharp

private void cbBeverage_SelectedIndexChanged(object sender, EventArgs e)
{
userInput selectedData = (userInput)cbBeverage.SelectedItem;
lblBeverage.Text = selectedData.Name + ":" + selectedData.Value;
beverage = selectedData.Value;
CalculateTotal();
}


You would do the exact same thing in the other methods but instead of using beverage you would use appe, main and dessert. Now, the method I was talking about will calculate the total of the order with the tax and assign it to a label on the form. I called the label on my form lblOrderTotal. This is what the CalculateTotal method looks like:

csharp

private void CalculateTotal()
{
total = beverage + appe + main + dessert;
tax = total * taxRate;
double orderTotal = total + tax;
lblOrderTotal.Text = orderTotal.ToString();
}


As you can see, this method takes each of the values and adds them together. Then it calculates the tax for the order. It adds the two together, converts it to a string and assigns it to the label.

If you can't get this to work on your own, leave a message and I will post the entire code for you.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 07:01PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month