⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addgas.cs

📁 PDA計算汽車平均油耗程式,使用C#程式及SQL CE 數據庫應用
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GasNote
{
    public partial class AddGas : Form
    {
        int _CarNo;
        int mode = 0;//0=Add,1=Edit
        DateTime _FillDate;
        decimal _ODO;
        decimal _Gas;
        decimal _Payment;
        int _ID;
        public AddGas(int CarNo)
        {
            InitializeComponent();
            _CarNo = CarNo;
            mode = 0;
        }


        public AddGas(int CarNo,DateTime FillDate,decimal ODO,decimal Gas,decimal Payment,int ID)
        {
            InitializeComponent();
            _CarNo = CarNo;
            _FillDate = FillDate;
            _ODO = ODO;
            _Gas = Gas;
            _Payment = Payment;
            _ID = ID;
            mode = 1;
        }

        string TxtInput = "";
        private void AddGas_Load(object sender, EventArgs e)
        {
            try
            {
                if (mode == 1)
                {
                    dtp_FillDate.Value = _FillDate;
                    txt_Gas.Text = _Gas.ToString();
                    txt_ODO.Text = _ODO.ToString();
                    txt_Payment.Text = _Payment.ToString();
                }
                else
                {
                    dtp_FillDate.Value = DateTime.Today;
                }
            }
            catch
            {
                this.DialogResult = DialogResult.Cancel;
            }
        }

        private void btn_1_Click(object sender, EventArgs e)
        {
            AddValue("1");
        }

        private void btn_2_Click(object sender, EventArgs e)
        {
            AddValue("2");
        }

        private void btn_3_Click(object sender, EventArgs e)
        {
            AddValue("3");
        }

        private void btn_4_Click(object sender, EventArgs e)
        {
            AddValue("4");
        }

        private void btn_5_Click(object sender, EventArgs e)
        {
            AddValue("5");
        }

        private void btn_6_Click(object sender, EventArgs e)
        {
            AddValue("6");
        }

        private void btn_7_Click(object sender, EventArgs e)
        {
            AddValue("7");
        }

        private void btn_8_Click(object sender, EventArgs e)
        {
            AddValue("8");
        }

        private void btn_9_Click(object sender, EventArgs e)
        {
            AddValue("9");
        }

        private void btn_0_Click(object sender, EventArgs e)
        {
            AddValue("0");
        }

        private void btn_dot_Click(object sender, EventArgs e)
        {
            AddValue(".");
        }

        private void btn_bs_Click(object sender, EventArgs e)
        {
            try
            {
                if (TxtInput == "Gas")
                {
                    if (txt_Gas.Text.Trim().Length > 0)
                    {
                        txt_Gas.Text = txt_Gas.Text.Remove(txt_Gas.Text.Trim().Length - 1, 1);
                    }
                }
                else if (TxtInput == "ODO")
                {
                    if (txt_ODO.Text.Trim().Length > 0)
                    {
                        txt_ODO.Text = txt_ODO.Text.Remove(txt_ODO.Text.Trim().Length - 1, 1);
                    }
                }
                else if (TxtInput == "Payment")
                {
                    if (txt_Payment.Text.Trim().Length > 0)
                    {
                        txt_Payment.Text = txt_Payment.Text.Remove(txt_Payment.Text.Trim().Length - 1, 1);
                    }
                }
            }
            catch
            {
                this.DialogResult = DialogResult.Cancel;
            }
        }

        private void AddValue(string NewNo)
        {
            try
            {
                if (TxtInput == "Gas")
                {
                    if (NewNo == "0")
                    {
                        if (txt_Gas.Text.Trim().Length == 0)
                        {
                            return;
                        }
                    }
                    if (NewNo == ".")
                    {
                        if (txt_Gas.Text.Trim().IndexOf(NewNo) > -1 || txt_Gas.Text.Trim().Length==0)
                        {
                            return;
                        }
                    }
                    txt_Gas.Text = txt_Gas.Text + NewNo;
                }
                else if (TxtInput == "ODO")
                {
                    if (NewNo == "0")
                    {
                        if (txt_ODO.Text.Trim().Length == 0)
                        {
                            return;
                        }
                    }
                    if (NewNo == ".")
                    {
                        if (txt_ODO.Text.Trim().IndexOf(NewNo) > -1 || txt_ODO.Text.Trim().Length == 0)
                        {
                            return;
                        }
                    }
                    txt_ODO.Text = txt_ODO.Text + NewNo;
                }
                else if (TxtInput == "Payment")
                {
                    if (NewNo == "0" )
                    {
                        if (txt_Payment.Text.Trim().Length == 0)
                        {
                            return;
                        }
                    }
                    if (NewNo == ".")
                    {
                        if (txt_Payment.Text.Trim().IndexOf(NewNo) > -1 || txt_Payment.Text.Trim().Length == 0)
                        {
                            return;
                        }
                    }
                    txt_Payment.Text = txt_Payment.Text + NewNo;
                }
            }
            catch
            {
                this.DialogResult = DialogResult.Cancel;
            }
        }

        private void txt_ODO_GotFocus(object sender, EventArgs e)
        {
            TxtInput = "ODO";
        }

        private void txt_Gas_GotFocus(object sender, EventArgs e)
        {
            TxtInput = "Gas";
        }

        private void txt_Payment_GotFocus(object sender, EventArgs e)
        {
            TxtInput = "Payment";
        }

        private void btn_Ok_Click(object sender, EventArgs e)
        {
            #region 檢查總里程
            if (txt_ODO.Text.Length == 0)
            {
                MessageBox.Show("請輸入總里程數");
                return;
            }
            try
            {
                decimal d = decimal.Parse(txt_ODO.Text.Trim());
                if (d == 0)
                {
                    MessageBox.Show("總里程數必需大於0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("總里程數僅接受數值\r\n");
                return;
            }
            #endregion

            #region 檢查加油量
            if (txt_Gas.Text.Length == 0)
            {
                MessageBox.Show("請輸入加油量");
                return;
            }
            try
            {
                decimal d = decimal.Parse(txt_Gas.Text.Trim());
                if (d == 0)
                {
                    MessageBox.Show("加油量必需大於0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("加油量僅接受數值\r\n");
                return;
            }
            #endregion

            #region 檢查油資
            if (txt_Payment.Text.Length == 0)
            {
                MessageBox.Show("請輸入油資");
                return;
            }
            try
            {
                decimal d = decimal.Parse(txt_Payment.Text.Trim());
                if (d == 0)
                {
                    MessageBox.Show("油資必需大於0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("油資僅接受數值\r\n");
                return;
            }
            #endregion

            DataAccess da = new DataAccess();
            try
            {
                if (mode == 1)
                {
                    da.EditGasList(dtp_FillDate.Value, txt_ODO.Text.Trim(), txt_Gas.Text.Trim(), txt_Payment.Text.Trim(), _CarNo,_ID);
                }
                else 
                {
                    da.AddGasList(dtp_FillDate.Value, txt_ODO.Text.Trim(), txt_Gas.Text.Trim(), txt_Payment.Text.Trim(), _CarNo);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.DialogResult = DialogResult.OK;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -