简体   繁体   中英

C#: Update values of readonly textboxes after each click

I'm trying to update values in read-only text boxes "Number of Months" and "Balance" after each click. In the given context both of these should have a starting value of 0 when the application begins. They should be updated after each click with the number of months simply adding up (1, 2, 3, etc.). The balance should show a specific value added up with each click.

The balance is equal to the monthly savings amount with the return of interest compounded monthly , so it would be dependent on the amount of months generated after each click.

I can make the month counter tick after each click as you'll see in the code, but I can't figure out how to update the balance and display it after each activation of the "Next Month" button.

public partial class frmSavingsCalculator : Form
{
    public frmSavingsCalculator()
    {
        InitializeComponent();
    }

    int months = 0;
    double balance = 0;

    private void btnNext_Click(object sender, EventArgs e)
    {
        double monthlypmt;
        int annualrate;

        if (double.TryParse(txtMonthlySavings.Text, out monthlypmt))
        {
            if (int.TryParse(txtAnnualInt.Text, out annualrate))
            {
                annualrate = annualrate / 100;
                double monthlyrate = annualrate / 12;
                double changerate = (1 + monthlyrate);

                txtMonths.Text = months.ToString();

                months++;

                balance = monthlypmt + Math.Pow(changerate, months);

                txtBalance.Text = balance.ToString("c");
                balance++;       
            }
            else
            {
                MessageBox.Show("Please insert valid interest rate.");
            }
        }
        else
        {
            MessageBox.Show("Please insert valid number.");
        }
    }

        private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

My form:

在此处输入图片说明

PS Here is the updated code, and yes I have additional problems!

namespace Assignment1_BTM380 { public partial class frmSavingsCalculator : Form { public frmSavingsCalculator() { InitializeComponent(); }

    int months = 0;
    double balance = 0;

    private void btnNext_Click(object sender, EventArgs e)
    {
        double monthlypmt;
        int annualrate;


        if (double.TryParse(txtMonthlySavings.Text, out monthlypmt))
        {
            if (int.TryParse(txtAnnualInt.Text, out annualrate))
            {
                double realannualrate = (double) annualrate / 100;
                double monthlyrate = realannualrate / 12;
                double interest = (monthlypmt * monthlyrate);

                txtMonths.Text = months.ToString();

                months++;

                balance = monthlypmt + balance * (Math.Pow(1+monthlyrate,months));



                txtBalance.Text = balance.ToString("c");




            }
            else
            {
                MessageBox.Show("Please insert valid interest rate.");
            }
        }
        else
        {
            MessageBox.Show("Please insert valid number.");
        }






    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

}

SO, if you have the patience, bear with me. I managed to get the two text boxes to react each time I click the "Next Month" button, only I can't seem to get the appropriate values in the Balance. According to my professor's example, after 12 months, the Balance should display $619.86, only it shows $868.06 in my Form. I think the problem is with the Balance calculation, which I hope some of you can comment on.

Thanks to everyone who took the time to reply!

You're actually doing it right, the problem is that you're always getting the same result after the initial calculation due to a variable type problem.

The problem is the fact that the local variable "annualrate" is Integer.

Initially it makes sense since you are forcing them to write an integer number in the text box. Yet problem arises when you make it an actual rate by dividing it by 100.

annualrate = annualrate / 100;

Unless the rate in the text box is greater or equal to 100 you will always get a rate of zero because mathematically the result of any number between 0 and 100 (not including 100) divided by 100 is always between 0 and 1 (not including 1). For example, 50% rate should give you 0.5 but will result into just 0 because of the Integer division.

An easy fix will be:

...
double realannualrate = annualrate / 100;
double monthlyrate = realannualrate / 12;
...

You might also want to consider moving calculations and initialization to a separate functions

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