• 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

  • 相关阅读:
    Linux 下安装 numpy 和 scipy
    排序算法总结
    机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)
    Java读取Excel并解析文本(并格式化输出)
    快捷键记忆
    初级应该学习的
    深入理解加密、解密、数字签名和数字证书
    数据签名和验签
    POI结构与常用类
    exlipse继承反编译插件
  • 原文地址:https://www.cnblogs.com/qingtianhua/p/3723099.html
Copyright © 2020-2023  润新知