簡體   English   中英

C# Windows 窗體貸款計算器

[英]C# Windows Form Loan Calculator

我想寫一個貸款計算器。 用戶可以選擇汽車型號,計算器將根據用戶輸入的首付和利率計算該汽車的每月分期付款。

但是當我單擊計算按鈕時,它會顯示一個消息框並說

輸入字符串的格式不正確

您可以檢查代碼底部的按鈕單擊事件。

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);
            }
        }
    }
}
            

從這樣的事情開始:

<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>

了解如何將其作為數據加載到內存中。 從那以后的一切都應該只是查找加載到內存中的數據。 您從此文件加載的數據不應該有Convert.To***代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM