Search code examples
c#textboxlistboxkiosk

I can't get the textBox_name text and listBox_order items to display correctly in one line on another listbox


I have 3 list boxes, the listBox_menu is where the items are first displayed if buttons (entrees, drinks, and sides) are clicked, then to add the items to order you select your item from the listBox_menu and click add to order to have it appear in the listBox_order. From there once the customer order is ready to be made the submit button will print out the customers name (textBox_name) and all the items in the listBox_order, and display it all in one line.

I'm just having trouble figuring out how to do that, if anyone could please help.

Code

Design


Solution

  • First define two global variables, then use the CalculateTotalCost function (which I defined).

    My answer has been updated.

    Output (tested in Visual Studio 2017, .Net Framework 4.5.2):

    Output

    These are my codes:

        public Form1()
        {
            InitializeComponent();
            listBox_menu.SelectionMode = SelectionMode.MultiExtended;
        }
        double Cost = 0;
        string Order = null;
        public double CalculateTotalCost(object input, bool Total)
        {
            if (Total == true)
            {
                switch (input)
                {
                    case "Salad":
                        Cost += 2500;
                        break;
                    case "Rice":
                        Cost += 3000;
                        break;
                    case "non-alcoholic beer":
                        Cost += 1000;
                        break;
                    case "Water":
                        Cost += 200;
                        break;
                    case "Ex1":
                        Cost += 2200;
                        break;
                    default:
                        Cost += 2200;
                        break;
                }
            }
            else
            {
                switch (input)
                {
                    case "Salad":
                        Cost -= 2500;
                        break;
                    case "Rice":
                        Cost -= 3000;
                        break;
                    case "non-alcoholic beer":
                        Cost -= 1000;
                        break;
                    case "Water":
                        Cost -= 200;
                        break;
                    case "Ex1":
                        Cost -= 2200;
                        break;
                    default:
                        Cost -= 2200;
                        break;
                }
            }
            return Cost;
        }
        private void Entrees_Click(object sender, EventArgs e)
        {
            listBox_menu.Items.Clear();
            listBox_menu.Items.Add("Salad");
            listBox_menu.Items.Add("Rice");
        }
        private void Drinks_Click(object sender, EventArgs e)
        {
            listBox_menu.Items.Clear();
            listBox_menu.Items.Add("non-alcoholic beer");
            listBox_menu.Items.Add("Water");
        }
        private void Sides_Click(object sender, EventArgs e)
        {
            listBox_menu.Items.Clear();
            listBox_menu.Items.Add("Ex1");
            listBox_menu.Items.Add("Ex2");
        }
        private void AddtoOrder_Click(object sender, EventArgs e)
        {
            if(listBox_menu.SelectedItems.Count>0)
            {
                for (int i = 0; i < listBox_menu.SelectedItems.Count; i++)
                {
                    listBox_order.Items.Add(listBox_menu.SelectedItems[i].ToString());
                    lblTotalCost.Text = (CalculateTotalCost(listBox_menu.SelectedItems[i].ToString(),true)).ToString();
                }
            }
        }
        private void RemoveFromOrder_Click(object sender, EventArgs e)
        {
            if (listBox_order.SelectedItems.Count > 0)
            {
                listBox_order.Items.Remove(listBox_order.SelectedItem);
            }
            Order = null;
            for (int i = 0; i < listBox_order.Items.Count; i++)
            {
                Order += listBox_order.Items[i].ToString() + " , ";
            }
            Cost = 0;
            if (listBox_order.Items.Count > 0)
            {
                for (int i = 0; i < listBox_order.Items.Count; i++)
                {
                    Cost = (CalculateTotalCost(listBox_order.Items[i], true));
                }
            }
            lblTotalCost.Text = Cost.ToString();
        }
        private void Submit_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Customer name: " + txtBoxCustomerName.Text + " Orders: " + Order + " Total cost: " + Cost);
        }