Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Windows.Forms;

namespace MajorProject1_2
{
    public partial class Form1 : Form
    {
        private double totalLoan;
        private double mounthsCounter;
        private string formatstring;
        private object accumulate1;
        private object accumulate2;
        private int months;
        private object answer;

        public double TotalInterest { get; private set; }
        public double TotalPP { get; private set; }
        public object Value { get; private set; }

        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            // select all of the Loan Amount textbox values when the user tabs up
            // use the keypress event to disallow anything but digits, control characters, and the period 

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            // accumulate the amount of all loans in a variable to later diplay in total loans label 
            accumulate1 = Value;
            accumulate2 = Value;

            // clear the list box 
             listBox1.Items.Clear();

            // clear all of the textboxes and combo box

            textBox1.Clear();
            comboBox2.Items.Clear();
            // set the rebate checkbox.checked property to false 
            checkBox1.Checked = false;
            // set the month 6 radio button as checked 
            if (radioButton1.Checked)
               months = 6;
            // disable the rebate textbox 
            textBox2.Enabled = false;
            // set the focus on the loan textbox 
            textBox1.Focus();








        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged_1(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {


            //declare variables 
            double loan, rate, rebate, months, principalPayment, interest, pmt;
                 
            try
            {
                double.TryParse(textBox1.Text, out loan);
                rate = Convert.ToDouble(comboBox2.Text);
                double.TryParse(textBox2.Text, out rebate);
                if (radioButton1.Checked)
                    months = 6;
                if (radioButton2.Checked)
                    months = 12;
                if (radioButton3.Checked)
                    months = 18;
                if (radioButton4.Checked)
                    months = 24;
                

            }
            catch {
                MessageBox.Show("Please select one from the box!", "Error Message",
                MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                groupBox1.Focus();
            }
            // get the intry from loan amount text box 
            double.TryParse(textBox1.Text, out loan);
            // code to read the APR value from the combo box and store it in the variable rate 
            rate = Convert.ToDouble(comboBox2.Text);
            // check for 0 values and run a message telling someone to put the correct input in 
            if (loan == 0)
            {
                MessageBox.Show("Please enter loan amount and annual APR.", "Error Message",
                    MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                textBox1.Focus();
            }
            if (rate == 0)
                MessageBox.Show("Please enter loan amount and annual APR.", "Error Message",
                       MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
            comboBox2.Focus();

            // use the checked method with the rebate checkbox and conditional logic
            if (checkBox1.Checked)
                double.TryParse(textBox2.Text, out rebate);


            //subtract the rebate amount from the loan amount 
            rebate = 0;
            loan = loan - rebate; 
            
            // convert the rate variable from a percent to a decimal (divide by 100) 
            rate = rate / 100;
            //calculate the principal payment amount using the following formula 
            months = 0; 

            principalPayment = rate / 12 * (loan + 0 * Math.Pow(1 + rate / 12, months)) / ((Math.Pow(1 + rate /
            12, months) - 1) * (1 + rate / 12 * 0));
            // calculate the monthly interest amount each month 
            interest = loan * rate / 12;
            // use this code to format the five columns in your listbox 
            string formatString = "{0,5}{1,12}{2,12}{3,12}{4,12}";


            // use a four loop to calculate the amortization schedule for the loan 
            
            //declare monthsCounter 
            for (int monthsCounter = 1; mounthsCounter <= months; monthsCounter = monthsCounter + 1)
            {
                pmt = principalPayment + interest;
                loan = principalPayment - loan - rebate;
                listBox1.Items.Add(String.Format(formatString, monthsCounter, interest.ToString("C"), principalPayment.ToString("N"), pmt.ToString("N"), loan.ToString("N")));

                totalLoan += loan;
                TotalInterest += interest;
                TotalPP += principalPayment;

                interest = loan * rate / 12;
                principalPayment = pmt - interest;

               
            }
            
            listBox1.Items.Add("{0,5}{1,12}{2,12}{3,12}{4,12}");
            listBox1.Items.Add(String.Format(formatString, interest.ToString("C"), principalPayment.ToString("N"), loan.ToString("N")));
            
            
            {

                listBox1.Items.Add("");
                listBox1.Items.Add(String.Format(formatString, interest.ToString("C"), principalPayment.ToString("N"), loan.ToString("N")));


            }

           





        }
       

        
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            // select all of the Loan Amount textbox values when the user tabs up; 
            comboBox2.SelectAll(); 
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // clear listbox if user makes any changes;         
            //listBox1.Items.Clear();
            
        }

        private void button4_Click(object sender, EventArgs e)
        {

            const string message = "Are you sure you want to exit?";
            const string caption = "Exit"; 
            var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                Application.Exit(); 
            }
            else if (result == DialogResult.Yes)
            {
                this.Close();
            }




        }

        private void textBox2_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            // write the amount of all loans (stored in a variable) total loans 
            double accumulate1 = double.Parse(textBox3.Text);

            // write the amount of all interest (store in variable) total interest 
            double accumulate2 = double.Parse(textBox4.Text);


        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
    }
}


What I have tried:

I have tried multiple ways to display code into the listbox but none of them are working.
Posted
Updated 8-Apr-22 17:10pm

1 solution

button1_Click never ends, your GUI thread remains busy since

C#
for (int monthsCounter = 1; mounthsCounter <= months; monthsCounter = monthsCounter + 1) {...}

is an eternal loop due to a typo.

Didn't you notice your app is dead in the water as soon as button1 is clicked?

:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900