Search code examples
c#winformsbuttoncurrencypos

How to add a money value to a Button in c# winforms?


I am making a Point of Sale (order taking) system. I would like to know how to assign a value to a button in c# in Winforms. For example, when you press the "meat lovers pizza" button, it appears in a Listbox with the name of the food item on the button and the price of the button.


Solution

  • To just add pizza name and price to ListBox all you need is to handle button click (create new handler by double clicking a button in form designer).

    private void PizzaButton_Click(object sender, EventArgs e)
    {
        MyListBox.Items.Add("Meat Lovers Pizza - $12");
    }
    

    But I guess you want to store orders somehow. One simple idea is to create a pizza class and store all ordered pizzas in some property.

        public partial class Form1 : Form
        {
            public List<Pizza> Pizzas { get; set; }
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void PizzaButton_Click(object sender, EventArgs e)
            {
                MyListBox.Items.Add("Meat Lovers Pizza - $12");
                Pizzas.Add(new Pizza()
                {
                    Name = "Meat Lovers Pizza",
                    Price = 12
                });
            }
        }
    
        public class Pizza
        {
            public string Name { get; set; }
            public double Price { get; set; }
        }
    

    This should get you started. You can later generate some kind of orders summary from that list of pizzas. Maybe create a list of available pizzas etc. Good luck.