Why is my code not displaying output in RichTextBox for windows forms Visual Studio? C#

Viewed 44

Can't figure out what I am doing wrong here to get my output to look like the example. I've attached the example output + what output I get. I don't have any bugs or errors I can find but is it something I did wrong with my loop? I'm fairly inexperienced with programming so apologies if this is a simple mistake.

Ideal output

My output

private double CalcPayment(double principal, double rate, int term)
            {
               double payment = (principal * rate) / (1 - (Math.Pow(rate + 1, -term)));
               return payment;
            }
    
            private void btnCalc_Click(object sender, EventArgs e)
            {
                //Input
                double principal = Convert.ToDouble(txtPrincipal.Text);
                double rate = Convert.ToDouble(txtRate.Text);
                int term = Convert.ToInt32(txtTerm.Text);
    
                //Processing
    
                rate /= 100;
                double balance = principal;
                double annualPayment = CalcPayment(principal, rate, term);
    
                rtbOut.Text = "Village of Muddy Bottom".PadLeft(15) + "\n" + "Bond Payment Schedule".PadLeft(16) + "\n";
    
                int yearsCounter;
                double annualInterest;
                double principalPaid;
                double totalInterest = 0;
    
                for (yearsCounter = 1; yearsCounter <= term; yearsCounter++)
                {
                    annualInterest = balance * rate;
                    principalPaid = annualPayment - annualInterest;
                    balance -= principalPaid;
                    totalInterest += annualInterest;
    
                    rtbOut.Text = term.ToString().PadLeft(5) + balance.ToString("c").PadLeft(20) + annualInterest.ToString("c").PadLeft(30) + principalPaid.ToString("c").PadLeft(40) + "\n";
                }
    
    
                //Postprocessing/Output
    
                rtbOut.Text = "Total" + totalInterest.ToString("c").PadLeft(40);
                lblPayment.Text = annualPayment.ToString("c");
            }
1 Answers

The problem is that you are overwriting the existing text at line

rtbOut.Text = "Total" + totalInterest.ToString("c").PadLeft(40);

That's why you are only displaying the total. You can fix that with concatenation. Just add a "+" everywhere you are assigning the text to a UI control.

If you are having problem also with lblPayment.Text do the same

private double CalcPayment(double principal, double rate, int term)
{
    double payment = (principal * rate) / (1 - (Math.Pow(rate + 1, -term)));
    return payment;
}

private void btnCalc_Click(object sender, EventArgs e)
{
    //Input
    double principal = Convert.ToDouble(txtPrincipal.Text);
    double rate = Convert.ToDouble(txtRate.Text);
    int term = Convert.ToInt32(txtTerm.Text);

    //Processing

    rate /= 100;
    double balance = principal;
    double annualPayment = CalcPayment(principal, rate, term);

    rtbOut.Text = "Village of Muddy Bottom".PadLeft(15) + "\n" + "Bond Payment Schedule".PadLeft(16) + "\n";

    int yearsCounter;
    double annualInterest;
    double principalPaid;
    double totalInterest = 0;

    for (yearsCounter = 1; yearsCounter <= term; yearsCounter++)
    {
        annualInterest = balance * rate;
        principalPaid = annualPayment - annualInterest;
        balance -= principalPaid;
        totalInterest += annualInterest;

        rtbOut.Text += term.ToString().PadLeft(5) + balance.ToString("c").PadLeft(20) + annualInterest.ToString("c").PadLeft(30) + principalPaid.ToString("c").PadLeft(40) + "\n";
    }


    //Postprocessing/Output

    rtbOut.Text += "Total" + totalInterest.ToString("c").PadLeft(40);
    lblPayment.Text = annualPayment.ToString("c");
}
Related