TextBox控件重寫
之NumTextBox,,主要實現(xiàn)的功能是,,只允許輸入數(shù)字,,或String,Numeric,Currency,Decimal,Float,Double,Short,Int,Long
等加了一個屬性InputType可以設(shè)置其,,還可以自己進(jìn)一步擴(kuò)展,因為是開源的,。
public enum NumTextBoxType {
String,//是這個的時候,,什么都不處理,跟正常TextBox一樣
Numeric,//只要是數(shù)字就行 Currency,
Decimal, Float, Double,
Short, Int, Long }
主要代碼:
- using System;
- using System.Windows.Forms;
- using System.ComponentModel;
- using System.Text.RegularExpressions;
-
- namespace YongFa365.Controls.NumTextBox
- {
- public class NumTextBox : TextBox
- {
- private NumTextBoxType inputType = NumTextBoxType.Numeric;
- public enum NumTextBoxType
- {
- String,
- Numeric,
- Currency,
- Decimal,
- Float,
- Double,
- Short,
- Int,
- Long
- }
-
- public NumTextBox()
- {
- this.ContextMenu = new ContextMenu();
- }
-
- [
- Category("專用設(shè)置"),
- DefaultValue(NumTextBoxType.Numeric),
- Description("設(shè)置允許類型:\nString跟普通TextBox功能一樣\nNumeric只要是數(shù)字就可以")
- ]
- public NumTextBoxType InputType
- {
- get { return inputType; }
- set { inputType = value; }
- }
-
- public override string Text
- {
- get { return base.Text; }
- set
- {
- if (IsValid(value, true))
- {
- base.Text = value;
- }
- }
- }
-
- private bool IsValid(string val, bool use)
- {
- bool ret = true;
-
- if (string.IsNullOrEmpty(val))
- {
- return ret;
- }
-
- if (use)
- {
- if (val.Equals("-") && inputType != NumTextBoxType.Numeric)
- {
- return ret;
- }
- }
-
-
- try
- {
- switch (inputType)
- {
- case NumTextBoxType.String:
- break;
- case NumTextBoxType.Numeric:
- if (!Regex.IsMatch(val, @"^\d*$"))
- {
- ret = false;
- }
- break;
- case NumTextBoxType.Currency:
- decimal dec = decimal.Parse(val);
- int pos = val.IndexOf(".");
- if (pos != -1)
- {
- ret = val.Substring(pos).Length <= 3;
- }
- break;
- case NumTextBoxType.Float:
- float flt = float.Parse(val);
- break;
- case NumTextBoxType.Double:
- double dbl = double.Parse(val);
- break;
- case NumTextBoxType.Decimal:
- decimal dec2 = decimal.Parse(val);
- break;
- case NumTextBoxType.Short:
- short s = short.Parse(val);
- break;
- case NumTextBoxType.Int:
- int i = int.Parse(val);
- break;
- case NumTextBoxType.Long:
- long l = long.Parse(val);
- break;
- default:
- throw new ApplicationException();
- }
- }
- catch
- {
- ret = false;
- }
- return ret;
- }
-
- protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
- {
-
- if (keyData == (Keys)Shortcut.CtrlV || keyData == (Keys)Shortcut.ShiftIns)
- {
- IDataObject iData = Clipboard.GetDataObject();
-
-
- string newText;
- newText = base.Text.Substring(0, base.SelectionStart)
- + (string)iData.GetData(DataFormats.Text)
- + base.Text.Substring(base.SelectionStart + base.SelectionLength);
-
- if (!IsValid(newText, true))
- {
- return true;
- }
- }
- return base.ProcessCmdKey(ref msg, keyData);
- }
-
- protected override void OnLeave(EventArgs e)
- {
- if (!(inputType == NumTextBoxType.Numeric || inputType == NumTextBoxType.String))
- {
- if (base.Text != "")
- {
- if (!IsValid(base.Text, false))
- {
- base.Text = "";
- }
- else if (Double.Parse(base.Text) == 0)
- {
- base.Text = "0";
- }
- }
-
- }
- base.OnLeave(e);
-
- }
-
- protected override void OnKeyPress(KeyPressEventArgs e)
- {
- if (inputType != NumTextBoxType.String)
- {
- char c = e.KeyChar;
- if (!Char.IsControl(c))
- {
- if (c.ToString() == " ")
- {
- e.Handled = true;
- return;
- }
-
- string newText = base.Text.Substring(0, base.SelectionStart)
- + c.ToString() + base.Text.Substring(base.SelectionStart + base.SelectionLength);
-
- if (!IsValid(newText, true))
- {
- e.Handled = true;
- }
- }
- }
- base.OnKeyPress(e);
- }
- }
- }
|