| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- 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;
- namespace WireProcessMachine
- {
- public partial class FrmKeybord : Form
- {
- static bool NumKeyboardEnable=true;
- private bool falg = false;
- private double MaxValue;
- private double MinValue;
- //public bool TypePassWord;
- private string str = "";
- #region
- public FrmKeybord()
- {
- 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 FrmKeybord(double maxValue, double minValue) : this()
- {
- this.MaxValue = maxValue;
- this.MinValue = minValue;
- lblMax.Text = maxValue.ToString();
- lblMin.Text = minValue.ToString();
- this.txtNumber.SelectAll();
- }
- public FrmKeybord(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)
- {
- }
- }
- #endregion
- }
|