zoukankan      html  css  js  c++  java
  • C# TextBox控件重写 之NumTextBox

    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
            }

    主要代码:

    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,//是这个的时候,什么都不处理,跟正常TextBox一样  
                Numeric,//只要是数字就行  
                Currency,  
                Decimal,  
                Float,  
                Double,  
                Short,  
                Int,  
                Long  
            }  
      
            public NumTextBox()  
            {  
                this.ContextMenu = new ContextMenu();  
            }  
      
            [  
            Category("专用设置"),  
            DefaultValue(NumTextBoxType.Numeric),  
            Description("设置允许类型:
    String跟普通TextBox功能一样
    Numeric只要是数字就可以")  
            ]  
            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);  
            }  
        }  
    }  

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------本文转载自:http://www.yongfa365.com/Item/NumTextBox.html

  • 相关阅读:
    【有奖互动】3D内容平台开版啦,参与互动话题赢超值京东卡!
    HarmonyOS开发竟如此简单,九岁小朋友也能轻松上手
    HMS Core 5.3.0版本发布公告
    Web页面如何进行可视化埋点(二)
    在鸿蒙中实现类似瀑布流效果
    手把手教你快应用接入微信H5网页支付
    Scrcpy投屏真机到电脑 并集成DevEco真机调试 Mac系统下
    【软通动力】HarmonyOS三方件开发指南(6)-ActiveOhos_sqlite组件
    Web页面如何进行可视化埋点(一)
    HMS Core Insights第三期直播预告—— 当我们在谈论App的时候,我们还可以谈论什么?
  • 原文地址:https://www.cnblogs.com/qingtianhua/p/3723099.html
Copyright © 2011-2022 走看看