csharp 發表於 2021-12-1 07:59:16

用元件設計的計算多年通膨每年費用之表格後端的程式碼

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 WindowsFormsApp48
{
    public partial class Form1 : Form
    {
      double yearSpend;
      
      double allYears;
      double flatRate;
      double sum = 0;
      public Form1()
      {
            InitializeComponent();
      }


      private void button1_Click(object sender, EventArgs e)
      {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
                MessageBox.Show("要輸入必要數字才能計算");
            else if (textBox1.Text.Any(c => c < 46 || c > 57||c==47)|| textBox2.Text.Any(c => c < 46 || c > 57 || c == 47) || textBox3.Text.Any(c => c < 46 || c > 57 || c == 47))//若填入的是數字或小數點以外,則顯示錯誤
                MessageBox.Show("只能輸入數字");
            else
            {
                sum = 0;
            yearSpend = 0;
            flatRate = 0;
            yearSpend = double.Parse(textBox1.Text);
            
                flatRate = double.Parse(textBox2.Text);
                allYears = (int)double.Parse(textBox3.Text);
                for (double i = 1; i <= allYears; i++)
                {
                  sum += yearSpend;
                  yearSpend *= (1 + flatRate / 100);

                }
                label9.Text = sum.ToString("0.00");
                label10.Text = allYears.ToString();
            }
      }

      private void button2_Click(object sender, EventArgs e)
      {
            yearSpend = 0;
            flatRate = 0;
            allYears = 0;
            sum = 0;
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            label9.Text = "";
            label10.Text = "";

      }

      private void button3_Click(object sender, EventArgs e)
      {
            yearSpend = 0;
            textBox1.Text = "";
            sum = 0;
      }

      private void button4_Click(object sender, EventArgs e)
      {
            
            flatRate = 0;
            textBox2.Text = "";
            sum = 0;
      }

      private void button5_Click(object sender, EventArgs e)
      {
         
            allYears = 0;
            textBox3.Text = "";
            sum = 0;
      }
    }
}
頁: [1]
查看完整版本: 用元件設計的計算多年通膨每年費用之表格後端的程式碼