Home > Software design >  C# Windows Form Loan Calculator
C# Windows Form Loan Calculator

Time:11-07

I want to write a loan calculator. A user gets to choose a car model, and the calculator will calculate the monthly installment for that car based on user input down payment and interest rate.

But when I click the calculate button it shows a message box and says

input string was not in a correct form

You can check the button click event at the bottom of the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Proton_System
{
    public partial class Loan_Calculator : Form
    {
        public Loan_Calculator()
        {
            InitializeComponent();
        }

        //Add Items into combobox2 and combobox4 when combobox1 is selected with specific car models
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem == "Persona")
            {
                comboBox2.Items.Clear();
                comboBox2.Items.Add("1.6L Standard CVT");
                comboBox2.Items.Add("1.6L Executive CVT");
                comboBox2.Items.Add("1.6L Premium CVT");

                comboBox4.Items.Clear();
                comboBox4.Items.Add("Peninsular Malaysia");
                comboBox4.Items.Add("East Malaysia");
                comboBox4.Items.Add("Labuan");
                comboBox4.Items.Add("Langkawi");
            }

            else if (comboBox1.SelectedItem == "X50")
            {
                comboBox2.Items.Clear();
                comboBox2.Items.Add("1.5T Standard");
                comboBox2.Items.Add("1.5T Executive");
                comboBox2.Items.Add("1.5T Premium");
                comboBox2.Items.Add("1.5TGDi Flagship");

                comboBox4.Items.Clear();
                comboBox4.Items.Add("Peninsular Malaysia");
                comboBox4.Items.Add("East Malaysia");
            }

            else
            {
                comboBox2.Items.Clear();
                comboBox2.Items.Add("1.8TGDi Standard");
                comboBox2.Items.Add("1.8TGDi Executive");
                comboBox2.Items.Add("1.8TGDi Premium");
                comboBox2.Items.Add("1.8TGDi Premium X");

                comboBox4.Items.Clear();
                comboBox4.Items.Add("Peninsular Malaysia");
                comboBox4.Items.Add("East Malaysia");
                comboBox4.Items.Add("Labuan");
                comboBox4.Items.Add("Langkawi");
            }
        }

        //Add Items into combobox3 when combobox2 is selected with specific variants
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedItem == "1.6L Standard CVT")
            {
                comboBox3.Items.Clear();
                comboBox3.Items.Add("Jet Grey");
                comboBox3.Items.Add("Armour Silver");
                comboBox3.Items.Add("Ruby Red");
                comboBox3.Items.Add("Rose Wood Maroon");
            }

            else if (comboBox2.SelectedItem == "1.6L Executive CVT")
            {
                comboBox3.Items.Clear();
                comboBox3.Items.Add("Snow White");
                comboBox3.Items.Add("Jet Grey");
                comboBox3.Items.Add("Armour Silver");
                comboBox3.Items.Add("Ruby Red");
                comboBox3.Items.Add("Rose Wood Maroon");

                
            }

            else if (comboBox2.SelectedItem == "1.6L Premium CVT")
            {
                comboBox3.Items.Clear();
                comboBox3.Items.Add("Snow White");
                comboBox3.Items.Add("Jet Grey");
                comboBox3.Items.Add("Armour Silver");
                comboBox3.Items.Add("Ruby Red");
                comboBox3.Items.Add("Rose Wood Maroon");
            }

            else if (comboBox2.SelectedItem == "1.5T Standard")
            {
                comboBox3.Items.Clear();
                comboBox3.Items.Add("Snow White");
                comboBox3.Items.Add("Jet Grey");
                comboBox3.Items.Add("Ocean Blue");
            }

            else if (comboBox2.SelectedItem == "1.5T Executive")
            {
                comboBox3.Items.Clear();
                comboBox3.Items.Add("Snow White");
                comboBox3.Items.Add("Jet Grey");
                comboBox3.Items.Add("Armour Silver");
                comboBox3.Items.Add("Critic Orange");
                comboBox3.Items.Add("Ocean Blue");
            }

            else if (comboBox2.SelectedItem == "1.5T Premium")
            {
                comboBox3.Items.Clear();
                comboBox3.Items.Add("Snow White");
                comboBox3.Items.Add("Jet Grey");
                comboBox3.Items.Add("Passion Red");
                comboBox3.Items.Add("Armour Silver");
                comboBox3.Items.Add("Critic Orange");
                comboBox3.Items.Add("Ocean Blue");
            }

            else if (comboBox2.SelectedItem == "1.5T Premium X")
            {
                comboBox3.Items.Clear();
                comboBox3.Items.Add("Snow White");
                comboBox3.Items.Add("Jet Grey");
                comboBox3.Items.Add("Passion Red");
                comboBox3.Items.Add("Armour Silver");
                comboBox3.Items.Add("Critic Orange");
                comboBox3.Items.Add("Ocean Blue");
            }

            else if (comboBox2.SelectedItem == "1.8TGDi Standard")
            {
                comboBox3.Items.Clear();
                comboBox3.Items.Add("Snow White");
                comboBox3.Items.Add("Jet Grey");
                comboBox3.Items.Add("Armour Silver");
            }

            else if (comboBox2.SelectedItem == "1.8TGDi Executive")
            {
                comboBox3.Items.Clear();
                comboBox3.Items.Add("Snow White");
                comboBox3.Items.Add("Jet Grey");
                comboBox3.Items.Add("Armour Silver");
                comboBox3.Items.Add("Ruby Red");
                comboBox3.Items.Add("Cinnamon Brown");
            }

            else if (comboBox2.SelectedItem == "1.8TGDi Premium")
            {
                comboBox3.Items.Clear();
                comboBox3.Items.Add("Snow White");
                comboBox3.Items.Add("Jet Grey");
                comboBox3.Items.Add("Armour Silver");
                comboBox3.Items.Add("Ruby Red");
                comboBox3.Items.Add("Space Grey");
                comboBox3.Items.Add("Cinnamon Brown");
            }

            else if (comboBox2.SelectedItem == "1.8TGDi Premium X")
            {
                comboBox3.Items.Clear();
                comboBox3.Items.Add("Snow White");
                comboBox3.Items.Add("Jet Grey");
                comboBox3.Items.Add("Armour Silver");
                comboBox3.Items.Add("Ruby Red");
                comboBox3.Items.Add("Space Grey");
                comboBox3.Items.Add("Cinnamon Brown");
            }
        }

        //Change label9 text when selected combobox4 and combobox2
        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Set label 9 text to  persona 1.6 standard price based on region choosen
            if (comboBox4.SelectedItem == "Peninsular Malaysia" && comboBox2.SelectedItem == "1.6L Standard CVT")
            {
                label9.Text = "RM45800";
            }
            else if (comboBox4.SelectedItem == "East Malaysia" && comboBox2.SelectedItem == "1.6L Standard CVT")
            {
                label9.Text = "RM47,800";
            }
            else if (comboBox4.SelectedItem == "Labuan" && comboBox2.SelectedItem == "1.6L Standard CVT")
            {
                label9.Text = "RM47,100";
            }
            else if (comboBox4.SelectedItem == "Langkawi" && comboBox2.SelectedItem == "1.6L Standard CVT")
            {
                label9.Text = "RM45,200";
            }

            //Set label 9 text to  persona 1.6 executive price based on region choosen
            else if (comboBox4.SelectedItem == "Peninsular Malaysia" && comboBox2.SelectedItem == "1.6L Executive CVT")
            {
                label9.Text = "RM50,800";
            }
            else if (comboBox4.SelectedItem == "East Malaysia" && comboBox2.SelectedItem == "1.6L Executive CVT")
            {
                label9.Text = "RM52,800";
            }
            else if (comboBox4.SelectedItem == "Labuan" && comboBox2.SelectedItem == "1.6L Executive CVT")
            {
                label9.Text = "RM51,200";
            }
            else if (comboBox4.SelectedItem == "Langkawi" && comboBox2.SelectedItem == "1.6L Executive CVT")
            {
                label9.Text = "RM49,400";
            }

            //Set label 9 text to  persona 1.6 premium price based on region choosen
            else if (comboBox4.SelectedItem == "Peninsular Malaysia" && comboBox2.SelectedItem == "1.6L Premium CVT")
            {
                label9.Text = "RM55,800";
            }
            else if (comboBox4.SelectedItem == "East Malaysia" && comboBox2.SelectedItem == "1.6L Premium CVT")
            {
                label9.Text = "RM57,800";
            }
            else if (comboBox4.SelectedItem == "Labuan" && comboBox2.SelectedItem == "1.6L Premium CVT")
            {
                label9.Text = "RM56,300";
            }
            else if (comboBox4.SelectedItem == "Langkawi" && comboBox2.SelectedItem == "1.6L Premium CVT")
            {
                label9.Text = "RM54,500";
            }

            //Set label 9 text to  X50 1.5T Standard price based on region choosen
            else if (comboBox4.SelectedItem == "Peninsular Malaysia" && comboBox2.SelectedItem == "1.5T Standard")
            {
                label9.Text = "RM79,200";
            }
            else if (comboBox4.SelectedItem == "East Malaysia" && comboBox2.SelectedItem == "1.5T Standard")
            {
                label9.Text = "RM81,200";
            }

            //Set label 9 text to  X50 1.5T Executive price based on region choosen
            else if (comboBox4.SelectedItem == "Peninsular Malaysia" && comboBox2.SelectedItem == "1.5T Executive")
            {
                label9.Text = "RM84,800";
            }
            else if (comboBox4.SelectedItem == "East Malaysia" && comboBox2.SelectedItem == "1.5T Executive")
            {
                label9.Text = "RM86,800";
            }

            //Set label 9 text to  X50 1.5T Premium price based on region choosen
            else if (comboBox4.SelectedItem == "Peninsular Malaysia" && comboBox2.SelectedItem == "1.5T Premium")
            {
                label9.Text = "RM93,200";
            }
            else if (comboBox4.SelectedItem == "East Malaysia" && comboBox2.SelectedItem == "1.5T Premium")
            {
                label9.Text = "RM95,200";
            }

            //Set label 9 text to  X50 1.5T Premium X price based on region choosen
            else if (comboBox4.SelectedItem == "Peninsular Malaysia" && comboBox2.SelectedItem == "1.5TGDi Flagship")
            {
                label9.Text = "RM103,300";
            }
            else if (comboBox4.SelectedItem == "East Malaysia" && comboBox2.SelectedItem == "1.5TGDi Flagship")
            {
                label9.Text = "RM105,300";
            }

            //Set label 9 text to  X70 1.8TGDi Standard price based on region choosen
            else if (comboBox4.SelectedItem == "Peninsular Malaysia" && comboBox2.SelectedItem == "1.8TGDi Standard")
            {
                label9.Text = "RM94,800";
            }
            else if (comboBox4.SelectedItem == "East Malaysia" && comboBox2.SelectedItem == "1.8TGDi Standard")
            {
                label9.Text = "RM94,800";
            }
            else if (comboBox4.SelectedItem == "Labuan" && comboBox2.SelectedItem == "1.8TGDi Standard")
            {
                label9.Text = "RM90,200";
            }
            else if (comboBox4.SelectedItem == "Langkawi" && comboBox2.SelectedItem == "1.8TGDi Standard")
            {
                label9.Text = "RM88,700";
            }

            //Set label 9 text to  X70 1.8TGDi Executive price based on region choosen
            else if (comboBox4.SelectedItem == "Peninsular Malaysia" && comboBox2.SelectedItem == "1.8TGDi Executive")
            {
                label9.Text = "RM106,800";
            }
            else if (comboBox4.SelectedItem == "East Malaysia" && comboBox2.SelectedItem == "1.8TGDi Executive")
            {
                label9.Text = "RM106,800";
            }
            else if (comboBox4.SelectedItem == "Labuan" && comboBox2.SelectedItem == "1.8TGDi Executive")
            {
                label9.Text = "RM101,800";
            }
            else if (comboBox4.SelectedItem == "Langkawi" && comboBox2.SelectedItem == "1.8TGDi Executive")
            {
                label9.Text = "RM100,300";
            }

            //Set label 9 text to  X70 1.8TGDi Premium price based on region choosen
            else if (comboBox4.SelectedItem == "Peninsular Malaysia" && comboBox2.SelectedItem == "1.8TGDi Premium")
            {
                label9.Text = "RM119,800";
            }
            else if (comboBox4.SelectedItem == "East Malaysia" && comboBox2.SelectedItem == "1.8TGDi Premium")
            {
                label9.Text = "RM119,800";
            }
            else if (comboBox4.SelectedItem == "Labuan" && comboBox2.SelectedItem == "1.8TGDi Premium")
            {
                label9.Text = "RM114,400";
            }
            else if (comboBox4.SelectedItem == "Langkawi" && comboBox2.SelectedItem == "1.8TGDi Premium")
            {
                label9.Text = "RM112,900";
            }

            //Set label 9 text to  X70 1.8TGDi Premium X price based on region choosen
            else if (comboBox4.SelectedItem == "Peninsular Malaysia" && comboBox2.SelectedItem == "1.8TGDi Premium X")
            {
                label9.Text = "RM122,800";
            }
            else if (comboBox4.SelectedItem == "East Malaysia" && comboBox2.SelectedItem == "1.8TGDi Premium X")
            {
                label9.Text = "RM122,800";
            }
            else if (comboBox4.SelectedItem == "Labuan" && comboBox2.SelectedItem == "1.8TGDi Premium X")
            {
                label9.Text = "RM117,300";
            }
            else if (comboBox4.SelectedItem == "Langkawi" && comboBox2.SelectedItem == "1.8TGDi Premium X")
            {
                label9.Text = "RM115,800";
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int amount, pay, afterprinciple, down, totalinterest, duration, year= 0;
            double rate= 0;
            try
            {
                rate = Convert.ToDouble(textBox2.Text);
                year = Convert.ToInt16(comboBox5.Text);
                duration = Convert.ToInt16(comboBox5.Text) * 12;
                amount = Convert.ToInt16(label9.Text);
                down = Convert.ToInt16(textBox1.Text);

                if (rate > 1)
                {
                    rate = rate / 100;
                }

                afterprinciple = amount - down;
                totalinterest = (int)(rate * (afterprinciple) * year);
                pay = (afterprinciple   totalinterest) / duration;

                label14.Text = pay.ToString();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
            

CodePudding user response:

Start with something like this:

<cars>
    <type name="Persona">
        <model name="1.6L Standard CVT">
            <location name="Peninsular Malaysia" price="45800"/>
            <location name="East Malaysia" price="47800"/>
            <location name="Labuan" price="47100"/>
            <location name="Langkawi" price="45200"/>
            <colour name="Jet Grey"/>
            <colour name="Armour Silver"/>
            <colour name="Ruby Red"/>
            <colour name="Rose Wood Maroon"/>
        </model>
        <model name="1.6L Executive CVT">
            <location name="Peninsular Malaysia" price="1"/>
            <location name="East Malaysia" price="1"/>
            <location name="Labuan" price="1"/>
            <location name="Langkawi" price="1"/>
            <colour name="Snow White"/>
            <colour name="Jet Grey"/>
            <colour name="Armour Silver"/>
            <colour name="Ruby Red"/>
            <colour name="Rose Wood Maroon"/>
        </model>
        <model name="1.6L Premium CVT">
            <location name="Peninsular Malaysia" price="1"/>
            <location name="East Malaysia" price="1"/>
            <location name="Labuan" price="1"/>
            <location name="Langkawi" price="1"/>
            <colour name="Snow White"/>
            <colour name="Jet Grey"/>
            <colour name="Armour Silver"/>
            <colour name="Ruby Red"/>
            <colour name="Rose Wood Maroon"/>
        </model>
    </type>
    <type name="X50">
        <model name="1.5T Standard">
            <location name="Peninsular Malaysia" price="1"/>
            <location name="East Malaysia" price="1"/>
            <colour name="Snow White"/>
            <colour name="Jet Grey"/>
            <colour name="Ocean Blue"/>
        </model>
        <model name="1.5T Executive">
            <location name="Peninsular Malaysia" price="1"/>
            <location name="East Malaysia" price="1"/>
            <colour name="Snow White"/>
            <colour name="Jet Grey"/>
            <colour name="Armour Silver"/>
            <colour name="Critic Orange"/>
            <colour name="Ocean Blue"/>
        </model>
        <model name="1.5T Premium">
            <location name="Peninsular Malaysia" price="1"/>
            <location name="East Malaysia" price="1"/>
            <colour name="Snow White"/>
            <colour name="Jet Grey"/>
            <colour name="Passion Red"/>
            <colour name="Armour Silver"/>
            <colour name="Critic Orange"/>
            <colour name="Ocean Blue"/>
        </model>
        <model name="1.5TGDi Flagship">
            <location name="Peninsular Malaysia" price="1"/>
            <location name="East Malaysia" price="1"/>
            <colour name="Snow White"/>
            <colour name="Jet Grey"/>
            <colour name="Passion Red"/>
            <colour name="Armour Silver"/>
            <colour name="Critic Orange"/>
            <colour name="Ocean Blue"/>
        </model>
    </type>
    <type name="???">
        <model name="1.8TGDi Standard">
            <location name="Peninsular Malaysia" price="1"/>
            <location name="East Malaysia" price="1"/>
            <location name="Labuan" price="1"/>
            <location name="Langkawi" price="1"/>
            <colour name="Snow White"/>
            <colour name="Jet Grey"/>
            <colour name="Armour Silver"/>
        </model>
        <model name="1.8TGDi Executive">
            <location name="Peninsular Malaysia" price="1"/>
            <location name="East Malaysia" price="1"/>
            <location name="Labuan" price="1"/>
            <location name="Langkawi" price="1"/>
            <colour name="Snow White"/>
            <colour name="Jet Grey"/>
            <colour name="Armour Silver"/>
            <colour name="Ruby Red"/>
            <colour name="Cinnamon Brown"/>
        </model>
        <model name="1.8TGDi Premium">
            <location name="Peninsular Malaysia" price="1"/>
            <location name="East Malaysia" price="1"/>
            <location name="Labuan" price="1"/>
            <location name="Langkawi" price="1"/>
            <colour name="Snow White"/>
            <colour name="Jet Grey"/>
            <colour name="Armour Silver"/>
            <colour name="Ruby Red"/>
            <colour name="Space Grey"/>
            <colour name="Cinnamon Brown"/>
        </model>
        <model name="1.8TGDi Premium X">
            <location name="Peninsular Malaysia" price="1"/>
            <location name="East Malaysia" price="1"/>
            <location name="Labuan" price="1"/>
            <location name="Langkawi" price="1"/>
            <colour name="Snow White"/>
            <colour name="Jet Grey"/>
            <colour name="Armour Silver"/>
            <colour name="Ruby Red"/>
            <colour name="Space Grey"/>
            <colour name="Cinnamon Brown"/>
        </model>
    </type>
</cars>

Learn how to load that into memory as data. Everything from there on should be just looking up the data that loaded into memory. There should be no Convert.To*** code for the data you've loaded from this file.

  • Related