每次按下按钮时如何计算

时间:2018-03-07 06:28:15

标签: c#

每次按下按钮而不必每次都输入新值时,如何更新计算?

 private void buttonNextMonth_Click(object sender, EventArgs e)
    {

        int month = 0;
        decimal monthlyRate, annualRate, monthlySavings, balance, initialSavings;

            annualRate = decimal.Parse(textBox2.Text);
            initialSavings = decimal.Parse(textBox1.Text);
            monthlyRate = annualRate / 12;
            monthlySavings = initialSavings * 1;
            balance = (monthlyRate * monthlySavings) + initialSavings;
            month++;

            textBox4.Text = balance.ToString();
            textBox3.Text = month.ToString();

1 个答案:

答案 0 :(得分:0)

将您的计数器置于点击事件之外,以便每次点击按钮时都不会启动计数器

喜欢这个

 int month = 0;
 private void buttonNextMonth_Click(object sender, EventArgs e)
 {
    decimal monthlyRate, annualRate, monthlySavings, balance, initialSavings;

        annualRate = decimal.Parse(textBox2.Text);
        initialSavings = decimal.Parse(textBox1.Text);
        monthlyRate = annualRate / 12;
        monthlySavings = initialSavings * 1;
        balance = (monthlyRate * monthlySavings) + initialSavings;
        month++;

        textBox4.Text = balance.ToString();
        textBox3.Text = month.ToString();