简体   繁体   中英

How to add value on button click C# winform

newbie question. I want my winform button to add the value on each click in a total textbox. for example if each click is 4.25 then 2 clicks would be 8.50. any information would be great.

private void BtnLarge_Click(object sender, EventArgs e){
        float largeC = 4.25F;
        TxbInvoice.Text += "Large Coffee......" + largeC + Environment.NewLine;
        txbtotal.Text += largeC++;
}

You can have a class level counter for click count and increment it on each click. Multiply the click counter with largeC will give you sum of total click multiplied by largeC which seem price of coffee.

int clickCount = 1;

private void BtnLarge_Click(object sender, EventArgs e)
{
    float largeC = 4.25F;
    TxbInvoice.Text += "Large Coffee......" + largeC + Environment.NewLine;
    txbtotal.Text += largeC * clickCount++;

}

You need to keep a running total in an instance variable outside of the method, then set the text of the text box to that value. Something like this.

public class frmMain
{
    private const float largeC = 4.25f;

    private float total;

    private void BtnLarge_Click(object sender, EventArgs e)
    {
        TxbInvoice.Text += "Large Coffee......" + largeC + Environment.NewLine;
        total += largeC;
        txbtotal.Text = total.ToString();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM