using Sunny.UI; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Design; using System.Linq; using System.Text; using System.Windows.Forms; using Permission; using Model; public enum NumberType { Integer=1, Double=2 } public partial class NumberKeybordForm : Form { TextBox txtInput; NumberType EditType = NumberType.Double; public bool ValueChanged { get; set; } public object Value { get; set; } public bool LimitChanged { get; set; } public double Maximum { get; set; }=int.MaxValue; public double Minimum { get; set; } = int.MinValue; //public PermissionLevel InputControlLevel { get; set; } = PermissionLevel.无权限; public NumberKeybordForm() { InitializeComponent(); lblPreviewValue.Text = ""; txtNumber.GotFocus += TxtNumber_GotFocus; txtMinimum.GotFocus += TxtMinimum_GotFocus; txtMaximum.GotFocus += TxtMaximum_GotFocus; } public NumberKeybordForm(object value) : this() { Value = value; lblPreviewValue.Text = value.ToString(); } public NumberKeybordForm(double maxValue, double minValue) : this() { txtMinimum.Text = minValue.ToString(); txtMaximum.Text = maxValue.ToString(); EditType = NumberType.Double; Value = 0; Maximum = maxValue; Minimum = minValue; } public NumberKeybordForm(double maxValue, double minValue, object currentValue) : this(maxValue, minValue) { Value = currentValue; lblPreviewValue.Text = currentValue.ToString(); } public NumberKeybordForm(int maxValue, int minValue) : this() { txtMinimum.Text = minValue.ToString(); txtMaximum.Text = maxValue.ToString(); EditType = NumberType.Integer; Value = 0; Maximum = maxValue; Minimum = minValue; } public NumberKeybordForm(int maxValue, int minValue, object currentValue) : this(maxValue, minValue) { Value = currentValue; lblPreviewValue.Text = currentValue.ToString(); } private void NumberKeybordForm_Load(object sender, EventArgs e) { txtInput = txtNumber; txtMinimum.TextChanged += txtMinimum_TextChanged; txtMaximum.TextChanged += txtMaximum_TextChanged; EnableEditLimitValue = PermissionManager.CurrentUser.PermissionLevel >= PermissionLevel.管理员; //PermissionHelper.SetPermissionLevel(txtMaximum, 3); //PermissionHelper.SetPermissionLevel(txtMinimum, 3); } private void txtMaximum_Click(object sender, EventArgs e) { txtMaximum.SelectAll(); } private void txtMinimum_Click(object sender, EventArgs e) { txtMinimum.SelectAll(); } private void txtNumber_Click(object sender, EventArgs e) { txtNumber.SelectAll(); } private void TxtMaximum_GotFocus(object sender, EventArgs e) { txtInput = txtMaximum; } private void TxtMinimum_GotFocus(object sender, EventArgs e) { txtInput = txtMinimum; } private void TxtNumber_GotFocus(object sender, EventArgs e) { txtInput = txtNumber; } private bool enableEditLimitValue=false; public bool EnableEditLimitValue { get => enableEditLimitValue; set { enableEditLimitValue = value; txtMinimum.Enabled = value; txtMaximum.Enabled = value; } } public bool IsPassword { set { if (value) txtNumber.PasswordChar = '*'; } } private void btnExit_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } private void btnNum0_Click(object sender, EventArgs e) { txtInput.Focus(); SimulateInput.SimulateKey(SimulateInput.VK_NUM0); } private void btnNum1_Click(object sender, EventArgs e) { txtInput.Focus(); SimulateInput.SimulateKey(SimulateInput.VK_NUM1); } private void btnNum2_Click(object sender, EventArgs e) { txtInput.Focus(); SimulateInput.SimulateKey(SimulateInput.VK_NUM2); } private void btnNum3_Click(object sender, EventArgs e) { txtInput.Focus(); SimulateInput.SimulateKey(SimulateInput.VK_NUM3); } private void btnNum4_Click(object sender, EventArgs e) { txtInput.Focus(); SimulateInput.SimulateKey(SimulateInput.VK_NUM4); } private void btnNum5_Click(object sender, EventArgs e) { txtInput.Focus(); SimulateInput.SimulateKey(SimulateInput.VK_NUM5); } private void btnNum6_Click(object sender, EventArgs e) { txtInput.Focus(); SimulateInput.SimulateKey(SimulateInput.VK_NUM6); } private void btnNum7_Click(object sender, EventArgs e) { txtInput.Focus(); SimulateInput.SimulateKey(SimulateInput.VK_NUM7); } private void btnNum8_Click(object sender, EventArgs e) { txtInput.Focus(); SimulateInput.SimulateKey(SimulateInput.VK_NUM8); } private void btnNum9_Click(object sender, EventArgs e) { txtInput.Focus(); SimulateInput.SimulateKey(SimulateInput.VK_NUM9); } private void btnPoint_Click(object sender, EventArgs e) { txtInput.Focus(); SimulateInput.SimulateKey(SimulateInput.VK_DOT); } private void btnClear_Click(object sender, EventArgs e) { txtInput.Focus(); txtInput.Text = "0"; txtInput.SelectionStart = 1; } private void btnDel_Click(object sender, EventArgs e) { txtInput.Focus(); SimulateInput.SimulateKey(SimulateInput.VK_BACKSPACE); } private void btnNegative_Click(object sender, EventArgs e) { txtInput.Focus(); if (txtInput.Text.Contains("-")) { txtInput.Text=txtInput.Text.Remove(0, 1); } else { txtInput.Text= txtInput.Text.Insert(0, "-"); } txtInput.SelectionStart = txtInput.Text.Length; } //string previewMaxString = "0"; private void txtMaximum_TextChanged(object sender, EventArgs e) { //if (!double.TryParse(txtMaximum.Text, out double currentValue)) //{ // txtMaximum.Text = previewMaxString; // txtMaximum.SelectionStart = txtMaximum.Text.Length; //} //else //{ // previewMaxString = txtMaximum.Text; //} } //string previewMinString = "0"; private void txtMinimum_TextChanged(object sender, EventArgs e) { //if (!double.TryParse(txtMinimum.Text, out double currentValue)) //{ // txtMinimum.Text = previewMinString; // txtMinimum.SelectionStart= txtMinimum.Text.Length; //} //else //{ // previewMinString = txtMinimum.Text; //} } //string previewNumberString = "0"; private void txtNumber_TextChanged(object sender, EventArgs e) { //if (!double.TryParse(txtNumber.Text, out double currentValue)) //{ // txtNumber.Text = previewNumberString; // txtNumber.SelectionStart = txtNumber.Text.Length; //} //else //{ // previewNumberString = txtNumber.Text; //} } private void btnEnter_Click(object sender, EventArgs e) { double maximum = 0; double minimum = 0; double input = 0; if ((txtNumber.Text.Length>0 && !double.TryParse(txtNumber.Text, out input)) || !double.TryParse(txtMaximum.Text, out maximum) || !double.TryParse(txtMinimum.Text, out minimum)) { MessageBox.Show("请输入正确的数值", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (EnableEditLimitValue && maximum < minimum) { MessageBox.Show("数值上限不能小于下限", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (txtNumber.Text.Length > 0 && (input > maximum || input < minimum)) { MessageBox.Show("输入值超限制", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (Maximum != maximum || Minimum != minimum) { LimitChanged = true; Maximum = maximum; Minimum = minimum; } if (txtNumber.Text.Length > 0 && Convert.ToDouble(Value.ToString()) != input) { ValueChanged = true; switch (EditType) { case NumberType.Integer: Value = (int)input; break; case NumberType.Double: Value = (double)input; break; } } DialogResult = DialogResult.OK; Close(); } }