267 lines
		
	
	
		
			7.6 KiB
		
	
	
	
		
			C#
		
	
			
		
		
	
	
			267 lines
		
	
	
		
			7.6 KiB
		
	
	
	
		
			C#
		
	
| using System;
 | |
| using System.Linq;
 | |
| using System.Collections.Generic;
 | |
| using System.ComponentModel;
 | |
| using System.Data;
 | |
| using System.Drawing;
 | |
| using System.Text;
 | |
| using System.Windows.Forms;
 | |
| using INT_LKD.DataStore;
 | |
| 
 | |
| namespace INT_LKD.DialogForms
 | |
| {
 | |
|     public partial class DialogFormNumKeyPad : Form
 | |
|     {
 | |
|         #region Field
 | |
|         private bool IsAddition;
 | |
|         private bool IsEditing;
 | |
|         private int Digit;
 | |
|         private int DecimalPlaces;
 | |
|         StringBuilder sbInputValue;
 | |
| 
 | |
|         private string m_StringValue;
 | |
|         #endregion
 | |
| 
 | |
|         #region Constructor
 | |
|         public DialogFormNumKeyPad(string caption, string value, int dig, int decimalPlaces, bool isSignEnable, Define.E_LanguageID language)
 | |
|         {
 | |
|             InitializeComponent();
 | |
| 
 | |
|             this.sbInputValue = new StringBuilder();
 | |
| 
 | |
|             this.labelCaption.Text = caption;
 | |
|             this.Digit = dig;
 | |
|             this.DecimalPlaces = decimalPlaces;
 | |
|             this.labelScreen.Text = value;
 | |
| 
 | |
|             if (value[0] == '-')
 | |
|                 this.IsAddition = false;
 | |
|             else
 | |
|                 this.IsAddition = true;
 | |
| 
 | |
|             this.buttonSign.Enabled = isSignEnable;
 | |
|             this.InitializeDesign(language);
 | |
|         }
 | |
|         public DialogFormNumKeyPad(string value, int dig, int decimalPlaces, bool isSignEnable, Define.E_LanguageID language)
 | |
|         {
 | |
|             InitializeComponent();
 | |
| 
 | |
|             this.sbInputValue = new StringBuilder();
 | |
| 
 | |
|             this.Digit = dig;
 | |
|             this.DecimalPlaces = decimalPlaces;
 | |
|             this.labelScreen.Text = value;
 | |
| 
 | |
|             if (value[0] == '-')
 | |
|                 this.IsAddition = false;
 | |
|             else
 | |
|                 this.IsAddition = true;
 | |
| 
 | |
|             this.buttonSign.Enabled = isSignEnable;
 | |
|             this.InitializeDesign(language);
 | |
|         }
 | |
|         #endregion
 | |
| 
 | |
|         #region Property
 | |
|         public string StringValue
 | |
|         {
 | |
|             get { return this.m_StringValue; }
 | |
|             private set { this.m_StringValue = value; }
 | |
|         }
 | |
| 
 | |
|         public double doubleValue
 | |
|         {
 | |
|             get
 | |
|             {
 | |
|                 double dValue = 0.0;
 | |
| 
 | |
|                 try
 | |
|                 {
 | |
|                     dValue = double.Parse(this.StringValue);
 | |
|                 }
 | |
|                 catch
 | |
|                 {
 | |
|                     dValue = -1;
 | |
|                 }
 | |
| 
 | |
|                 return dValue;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public int IntValue
 | |
|         {
 | |
|             get { return int.Parse(this.StringValue); }
 | |
|         }
 | |
|         #endregion
 | |
| 
 | |
|         #region Method
 | |
|         private void InitializeContnrol()
 | |
|         {
 | |
|             int x = 0, y = 0;
 | |
| 
 | |
|             x = Screen.PrimaryScreen.Bounds.Width / 2 - this.Size.Width / 2;
 | |
|             y = Screen.PrimaryScreen.Bounds.Height / 2 - this.Size.Height / 2;
 | |
| 
 | |
|             this.Location = new Point(x, y);
 | |
| 
 | |
|             this.Size = new Size(250, 415);
 | |
|         }
 | |
|         private void InitializeDesign(Define.E_LanguageID language)
 | |
|         {
 | |
|             if (language == Define.E_LanguageID.English)
 | |
|             {
 | |
|                 this.buttonCancel.Text = "ESC";
 | |
|                 this.buttonEnter.Text = "Enter";
 | |
|             }
 | |
|             else if (language == Define.E_LanguageID.Chinese)
 | |
|             {
 | |
|                 this.labelCaption.Text = "键盘";
 | |
| 
 | |
|                 this.buttonCancel.Text = "取消";
 | |
|                 this.buttonEnter.Text = "确认";
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 this.buttonCancel.Text = "ESC";
 | |
|                 this.buttonEnter.Text = "Enter";
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         private void InputNumber(string key)
 | |
|         {
 | |
|             if (this.IsEditing == false)
 | |
|                 this.IsEditing = true;
 | |
| 
 | |
|             this.sbInputValue.Append(key);
 | |
|             this.DisplayScreen(this.sbInputValue.ToString());
 | |
|         }
 | |
| 
 | |
|         private void DisplayScreen(string value)
 | |
|         {
 | |
|             int iValue = 0;
 | |
|             string sValue = "";
 | |
| 
 | |
|             if (this.IsEditing == false || value == null)
 | |
|                 return;
 | |
| 
 | |
|             iValue = int.Parse(value);
 | |
| 
 | |
|             if (this.DecimalPlaces == 0)
 | |
|             {
 | |
|                 if (iValue.ToString().Length <= this.Digit)
 | |
|                     sValue = iValue.ToString();
 | |
|                 else
 | |
|                 {
 | |
|                     this.sbInputValue.Length = 0;
 | |
|                     sValue = "0";
 | |
|                 }
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 if (iValue.ToString().Length <= this.Digit)
 | |
|                     sValue = Helper.StringToDecimalPlaces(iValue.ToString(), this.DecimalPlaces);
 | |
|                 else
 | |
|                 {
 | |
|                     this.sbInputValue.Length = 0;
 | |
|                     sValue = Helper.StringToDecimalPlaces(this.sbInputValue.ToString(), this.DecimalPlaces);
 | |
|                 }
 | |
|             }
 | |
| 
 | |
|             if (this.IsAddition == true)
 | |
|                 this.labelScreen.Text = string.Format("{0}", sValue);
 | |
|             else
 | |
|                 this.labelScreen.Text = string.Format("-{0}", sValue);
 | |
|         }
 | |
|         #endregion
 | |
| 
 | |
|         #region Override Member
 | |
|         protected override void OnLoad(EventArgs e)
 | |
|         {
 | |
|             base.OnLoad(e);
 | |
| 
 | |
|             this.InitializeContnrol();
 | |
|         }
 | |
|         #endregion
 | |
| 
 | |
|         #region Event Handler
 | |
|         private void buttonNumber_Click(object sender, EventArgs e)
 | |
|         {
 | |
|             Button bt = sender as Button;
 | |
| 
 | |
|             if (bt == null)
 | |
|                 return;
 | |
| 
 | |
|             if (bt == this.buttonNumber0)
 | |
|                 this.InputNumber("0");
 | |
|             else if (bt == this.buttonNumber1)
 | |
|                 this.InputNumber("1");
 | |
|             else if (bt == this.buttonNumber2)
 | |
|                 this.InputNumber("2");
 | |
|             else if (bt == this.buttonNumber3)
 | |
|                 this.InputNumber("3");
 | |
|             else if (bt == this.buttonNumber4)
 | |
|                 this.InputNumber("4");
 | |
|             else if (bt == this.buttonNumber5)
 | |
|                 this.InputNumber("5");
 | |
|             else if (bt == this.buttonNumber6)
 | |
|                 this.InputNumber("6");
 | |
|             else if (bt == this.buttonNumber7)
 | |
|                 this.InputNumber("7");
 | |
|             else if (bt == this.buttonNumber8)
 | |
|                 this.InputNumber("8");
 | |
|             else if (bt == this.buttonNumber9)
 | |
|                 this.InputNumber("9");
 | |
|         }
 | |
| 
 | |
|         private void buttonCancel_Click(object sender, EventArgs e)
 | |
|         {
 | |
|             this.Close();
 | |
|         }
 | |
| 
 | |
|         private void buttonEnter_Click(object sender, EventArgs e)
 | |
|         {
 | |
|             this.StringValue = this.labelScreen.Text;
 | |
| 
 | |
|             if (this.doubleValue == 0)
 | |
|             {
 | |
|                 if (this.DecimalPlaces == 1)
 | |
|                     this.StringValue = "0.0";
 | |
|                 else if (this.DecimalPlaces == 2)
 | |
|                     this.StringValue = "0.00";
 | |
|                 else if (this.DecimalPlaces == 3)
 | |
|                     this.StringValue = "0.000";
 | |
|                 else
 | |
|                     this.StringValue = "0";
 | |
|             }
 | |
| 
 | |
|             this.DialogResult = DialogResult.OK;
 | |
|         }
 | |
| 
 | |
|         private void buttonSign_Click(object sender, EventArgs e)
 | |
|         {
 | |
|             if (this.IsEditing == false)
 | |
|             {
 | |
|                 if (this.IsAddition == false)
 | |
|                 {
 | |
|                     this.IsAddition = true;
 | |
|                     this.labelScreen.Text = this.labelScreen.Text.Remove(0, 1);
 | |
|                 }
 | |
|                 else
 | |
|                 {
 | |
|                     this.IsAddition = false;
 | |
|                     this.labelScreen.Text = this.labelScreen.Text.Insert(0, "-");
 | |
|                 }
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 if (this.IsAddition == false)
 | |
|                     this.IsAddition = true;
 | |
|                 else
 | |
|                     this.IsAddition = false;
 | |
| 
 | |
|                 this.DisplayScreen(this.sbInputValue.ToString());
 | |
|             }
 | |
|         }
 | |
|         #endregion
 | |
|     }
 | |
| } |