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


public partial class NumKeybordForm : Form
{
    static bool NumKeyboardEnable = true;
    private bool falg = false;
    private double MaxValue;
    private double MinValue;
    //public bool TypePassWord;
    private string str = "";
    public NumKeybordForm()
    {
        InitializeComponent();
        this.ControlBox = false;
        this.MaximizeBox = false;
        foreach (Control item in panel1.Controls)
        {
            if (item is Button)
            {
                Button btn = (Button)item;
                btn.Click += btnNumber_Click;
            }
        }
        btnSub.Click -= btnNumber_Click;
    }

    public new DialogResult ShowDialog()
    {
        if (NumKeyboardEnable == false)
        {
            this.Tag = str;
            return DialogResult.Abort;
        }
        else
            return base.ShowDialog();
    }

    public bool TypePassword
    {
        set
        {
            if (value)
                txtNumber.PasswordChar = '*';
        }
    }

    public NumKeybordForm(double maxValue, double minValue) : this()
    {
        this.MaxValue = maxValue;
        this.MinValue = minValue;
        lblMax.Text = maxValue.ToString();
        lblMin.Text = minValue.ToString();
        this.txtNumber.SelectAll();
    }

    public NumKeybordForm(double maxValue, double minValue, string text) : this()
    {
        this.MaxValue = maxValue;
        this.MinValue = minValue;
        if (text == null || text == "")
        {
            this.txtNumber.Text = "0";
            str = "";
        }
        else
        {
            this.txtNumber.Text = text;
            str = text;
        }
        lblMax.Text = maxValue.ToString();
        lblMin.Text = minValue.ToString();
        this.txtNumber.SelectAll();
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Tag = str;
        this.DialogResult = DialogResult.Cancel;
        this.Close();
    }

    private void FrmKeybord_FormClosing(object sender, FormClosingEventArgs e)
    {

    }
    private void btnNumber_Click(object sender, EventArgs e)
    {
        if (falg == false)
        {
            this.txtNumber.Text = "";
            falg = true;
        }
        if (((Button)sender).Text != ".")
        {
            if (txtNumber.Text.Substring(0) == "0")
            {
                txtNumber.Text = txtNumber.Text.Substring(1);
            }
            this.txtNumber.Text += ((Button)sender).Text;
        }
        else
        {
            if (((Button)sender).Text == "." && !this.txtNumber.Text.Contains("."))
            {

                if (txtNumber.Text.Substring(0) == "0")
                {
                    this.txtNumber.Text = "0" + ((Button)sender).Text;
                    //txtNumber.Text = txtNumber.Text.Substring(1);
                }
                else if (txtNumber.Text.Substring(0) == "")
                {
                    this.txtNumber.Text = "0" + ((Button)sender).Text;
                }
                else
                {
                    this.txtNumber.Text += ((Button)sender).Text;
                }

            }

        }
    }
    #region 输入数据
    private void btnEnter_Click(object sender, EventArgs e)
    {
        double number = 0;
        double.TryParse(this.txtNumber.Text, out number);
        double x = this.txtNumber.Text == "" ? 0 : number;
        if (x > this.MaxValue || x < this.MinValue)
        {
            DialogResult result = MessageBox.Show("输入数值范围错误！重新输入！" + "\r\n" + "Over the value limit!",
                   "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                this.txtNumber.Focus();
                this.txtNumber.SelectAll();
                falg = false;
                return;
            }

        }
        else
        {
            ReturnValue();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

    }
    #endregion
    private void ReturnValue()
    {
        if (this.txtNumber.Text.Length > 1 && !this.txtNumber.Text.Contains("-"))
        {
            this.Tag = this.txtNumber.Text;
        }
        else
        {
            if (this.txtNumber.Text.Length > 1 && this.txtNumber.Text.Contains("-"))
            {
                this.Tag = "-" + this.txtNumber.Text.Substring(1, this.txtNumber.Text.Length - 1);
            }
            else if (this.txtNumber.Text.Length == 1)
            {
                this.Tag = this.txtNumber.Text;
            }

        }
    }
    #region 删除一个数字
    private void btnDel_Click(object sender, EventArgs e)
    {
        if (txtNumber.Text.Length > 1)
        {
            txtNumber.Text = txtNumber.Text.Substring(0, txtNumber.Text.Length - 1);
        }
        else
        {
            txtNumber.Text = "0";
        }
    }
    #endregion
    #region 清除数字
    private void btnClear_Click(object sender, EventArgs e)
    {
        this.txtNumber.Text = "";
    }
    #endregion
    #region 添加正负号
    private void btnSub_Click(object sender, EventArgs e)
    {
        if (falg == false)
        {
            this.txtNumber.Text = "";
            falg = true;
        }
        if (!txtNumber.Text.Contains("-"))
        {
            if (txtNumber.Text.Substring(0) == "0")
            {
                txtNumber.Text = txtNumber.Text.Substring(1);
            }
            this.txtNumber.Text = "-" + this.txtNumber.Text;
        }
        else
        {
            if (txtNumber.Text.Length > 1)
            {
                txtNumber.Text = txtNumber.Text.Substring(1, txtNumber.Text.Length - 1);
            }
        }
    }
    #endregion

    private void FrmKeybord_FormClosed(object sender, FormClosedEventArgs e)
    {

    }
}