简体   繁体   English

如何在按钮上单击C#Winform来增加价值

[英]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. 我希望我的winform按钮在总文本框中为每次单击添加值。 for example if each click is 4.25 then 2 clicks would be 8.50. 例如,如果每次点击为4.25,则2次点击为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. 将点击计数器乘以largeC,即可得出总点击次数乘以largeC(即咖啡的价格)的总和。

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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