[.net自定义控件]TextBox控件重写 之NumTextBox

来源:百度文库 编辑:神马文学网 时间:2024/05/24 01:21:52

TextBox控件重写 之NumTextBox,主要实现的功能是,只允许输入数字,或String,Numeric,Currency,Decimal,Float,Double,Short,Int,Long

等加了一个属性InputType可以设置其,还可以自己进一步扩展,因为是开源的。

        public enum NumTextBoxType
        {
            String,//是这个的时候,什么都不处理,跟正常TextBox一样
            Numeric,//只要是数字就行
            Currency,
            Decimal,
            Float,
            Double,
            Short,
            Int,
            Long
        }

主要代码:

view plaincopy to clipboardprint?
  1. using System;   
  2. using System.Windows.Forms;   
  3. using System.ComponentModel;   
  4. using System.Text.RegularExpressions;   
  5.   
  6. namespace YongFa365.Controls.NumTextBox   
  7. {   
  8.     public class NumTextBox : TextBox   
  9.     {   
  10.         private NumTextBoxType inputType = NumTextBoxType.Numeric;   
  11.         public enum NumTextBoxType   
  12.         {   
  13.             String,//是这个的时候,什么都不处理,跟正常TextBox一样   
  14.             Numeric,//只要是数字就行   
  15.             Currency,   
  16.             Decimal,   
  17.             Float,   
  18.             Double,   
  19.             Short,   
  20.             Int,   
  21.             Long   
  22.         }   
  23.   
  24.         public NumTextBox()   
  25.         {   
  26.             this.ContextMenu = new ContextMenu();   
  27.         }   
  28.   
  29.         [   
  30.         Category("专用设置"),   
  31.         DefaultValue(NumTextBoxType.Numeric),   
  32.         Description("设置允许类型:\nString跟普通TextBox功能一样\nNumeric只要是数字就可以")   
  33.         ]   
  34.         public NumTextBoxType InputType   
  35.         {   
  36.             get { return inputType; }   
  37.             set { inputType = value; }   
  38.         }   
  39.   
  40.         public override string Text   
  41.         {   
  42.             get { return base.Text; }   
  43.             set  
  44.             {   
  45.                 if (IsValid(value, true))   
  46.                 {   
  47.                     base.Text = value;   
  48.                 }   
  49.             }   
  50.         }   
  51.   
  52.         private bool IsValid(string val, bool use)   
  53.         {   
  54.             bool ret = true;   
  55.   
  56.             if (string.IsNullOrEmpty(val))   
  57.             {   
  58.                 return ret;   
  59.             }   
  60.   
  61.             if (use)   
  62.             {   
  63.                 if (val.Equals("-") && inputType != NumTextBoxType.Numeric)   
  64.                 {   
  65.                     return ret;   
  66.                 }   
  67.             }   
  68.   
  69.   
  70.             try  
  71.             {   
  72.                 switch (inputType)   
  73.                 {   
  74.                     case NumTextBoxType.String:   
  75.                         break;   
  76.                     case NumTextBoxType.Numeric:   
  77.                         if (!Regex.IsMatch(val, @"^\d*$"))   
  78.                         {   
  79.                             ret = false;   
  80.                         }   
  81.                         break;   
  82.                     case NumTextBoxType.Currency:   
  83.                         decimal dec = decimal.Parse(val);   
  84.                         int pos = val.IndexOf(".");   
  85.                         if (pos != -1)   
  86.                         {   
  87.                             ret = val.Substring(pos).Length <= 3;   
  88.                         }   
  89.                         break;   
  90.                     case NumTextBoxType.Float:   
  91.                         float flt = float.Parse(val);   
  92.                         break;   
  93.                     case NumTextBoxType.Double:   
  94.                         double dbl = double.Parse(val);   
  95.                         break;   
  96.                     case NumTextBoxType.Decimal:   
  97.                         decimal dec2 = decimal.Parse(val);   
  98.                         break;   
  99.                     case NumTextBoxType.Short:   
  100.                         short s = short.Parse(val);   
  101.                         break;   
  102.                     case NumTextBoxType.Int:   
  103.                         int i = int.Parse(val);   
  104.                         break;   
  105.                     case NumTextBoxType.Long:   
  106.                         long l = long.Parse(val);   
  107.                         break;   
  108.                     default:   
  109.                         throw new ApplicationException();   
  110.                 }   
  111.             }   
  112.             catch  
  113.             {   
  114.                 ret = false;   
  115.             }   
  116.             return ret;   
  117.         }   
  118.   
  119.         protected override bool ProcessCmdKey(ref Message msg, Keys keyData)   
  120.         {   
  121.   
  122.             if (keyData == (Keys)Shortcut.CtrlV || keyData == (Keys)Shortcut.ShiftIns)   
  123.             {   
  124.                 IDataObject iData = Clipboard.GetDataObject();   
  125.   
  126.   
  127.                 string newText;   
  128.                 newText = base.Text.Substring(0, base.SelectionStart)   
  129.                     + (string)iData.GetData(DataFormats.Text)   
  130.                     + base.Text.Substring(base.SelectionStart + base.SelectionLength);   
  131.   
  132.                 if (!IsValid(newText, true))   
  133.                 {   
  134.                     return true;   
  135.                 }   
  136.             }   
  137.             return base.ProcessCmdKey(ref msg, keyData);   
  138.         }   
  139.   
  140.         protected override void OnLeave(EventArgs e)   
  141.         {   
  142.             if (!(inputType == NumTextBoxType.Numeric || inputType == NumTextBoxType.String))   
  143.             {   
  144.                 if (base.Text != "")   
  145.                 {   
  146.                     if (!IsValid(base.Text, false))   
  147.                     {   
  148.                         base.Text = "";   
  149.                     }   
  150.                     else if (Double.Parse(base.Text) == 0)   
  151.                     {   
  152.                         base.Text = "0";   
  153.                     }   
  154.                 }   
  155.   
  156.             }   
  157.             base.OnLeave(e);   
  158.   
  159.         }   
  160.   
  161.         protected override void OnKeyPress(KeyPressEventArgs e)   
  162.         {   
  163.             if (inputType != NumTextBoxType.String)   
  164.             {   
  165.                 char c = e.KeyChar;   
  166.                 if (!Char.IsControl(c))   
  167.                 {   
  168.                     if (c.ToString() == " ")   
  169.                     {   
  170.                         e.Handled = true;   
  171.                         return;   
  172.                     }   
  173.   
  174.                     string newText = base.Text.Substring(0, base.SelectionStart)   
  175.                         + c.ToString() + base.Text.Substring(base.SelectionStart + base.SelectionLength);   
  176.   
  177.                     if (!IsValid(newText, true))   
  178.                     {   
  179.                         e.Handled = true;   
  180.                     }   
  181.                 }   
  182.             }   
  183.             base.OnKeyPress(e);   
  184.         }   
  185.     }