So im actually creating a fastfood menu in which save how many times a button been clicked as a variable. So I can multiply it to its value. For example if i click btnChicken a variable will count how many btnChicken been clicked then it will multplied on the amount i set for the value of each chicken.

Dani AI

Generated

The original report describes a simple fast‑food menu where each item button increments a counter and the UI shows line totals and a grand total. Because the project type is unclear (native C++ vs C++/CLI WinForms vs ASP.NET), the event and state model matters; as pointed out, confirming the project type is the first step. Building on 's suggestion to centralize counting and 's note about allowing decreases, a small data model plus a single handler pattern keeps logic tidy and avoids duplicated code.

A compact, proven pattern (easy to translate between C++/CLI and C#) is: keep two dictionaries (price and quantity), store a product key in each button's Tag, assign the same Click handler to every item button, and implement a separate Decrease handler. Totals are recalculated in one UpdateTotals() routine.

// fields on the form
private Dictionary<string,int> qty = new Dictionary<string,int>();
private Dictionary<string,decimal> price = new Dictionary<string,decimal>();

// initialization example
price["Chicken"] = 4.50m;

// wire buttons: btnChicken.Tag = "Chicken"; btnChicken.Click += Item_Click;

private void Item_Click(object sender, EventArgs e)
{
    var key = (string)((Button)sender).Tag;
    if (!qty.TryGetValue(key, out var v)) v = 0;
    qty[key] = v + 1;
    UpdateTotals();
}

private void Decrease_Click(object sender, EventArgs e)
{
    var key = (string)((Button)sender).Tag;
    if (qty.TryGetValue(key, out var v) && v > 0) qty[key] = v - 1;
    UpdateTotals();
}

private void UpdateTotals()
{
    decimal total = 0m;
    foreach (var p in price)
    {
        qty.TryGetValue(p.Key, out var q);
        total += p.Value * q;
    }
    lblTotal.Text = total.ToString("C");
}

Troubleshooting and best practices: prefer fixed-point for money (decimal or integer cents) to avoid rounding errors; offer a NumericUpDown or a decrement button so quantities can be corrected (echoing ); if this is a WebForms app, recreate dynamic controls early (Page_Init) so events and state persist across postbacks; persistent storage choices (session, localStorage or a database) depend on whether the data must survive reloads, as mentioned. Debugging hints: set breakpoints in the shared handlers, inspect the Tag value, and confirm the dictionaries live at form scope (not recreated inside the handler).

Recommended Answers

All 5 Replies

As c++ doesn't have a GUI system I'd be guessing if this was in Visual Studio C++ or something else.

Even so, you would implement your code to that button's handler to do what you wrote above.

With a vague description and no code supplied it is almost impossible to figure out exactly what you want to do.

If the task is to count "every" click probably you may want to consider creating a user control.

Your code will look something like this -

int count = 1;

private void Page_Load(object sender, System.EventArgs e)
{
    Button btn = new Button();
    btn.Text = "Click Me";
    btn.Click  += btn_Click;
    lbl = new Label();
    form1.Controls.Add(btn);
    form1.Controls.Add(lbl);
}

protected void btn_Click(object sender, EventArgs e)
{
    count++;
    if(lbl !=null)
      lbl.Text = count.ToString();            
}

Good topic and some valid comments. Don't forget to display the quantity (number) each time the button is pressed, and there should also be a way to reduce the number if the customer changes his/her mind, or presses the button too many times by mistake.

Interesting topic with good replies. I'm just adding something very general as I work in a completely different area most of the year and want to avoid getting too rusty.
I'd start from the question: Who are you storing the data for, you, the client or both?
For you it's a database, for clients think javascript and local storage.
Are you sure you didn't mean .NET and C# ? Try Visual Studio.

It's always hard in the beginning just keep on doing it.
I remember when I had some problems with that.. .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.