textbox 输入限制

来源:互联网 发布:天刀捏脸数据男黄晓明 编辑:程序博客网 时间:2024/06/07 11:13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication10
{
    /// <summary>
    /// 属性设定功能
    /// CusType:指定限制输入类型分为 整数、双精度、英文、英数
    /// IsFu:输入设定为整数、双精度时,是否可输入负号
    /// MaxValue:输入设定为整数、双精度时,数的最大值
    /// DotCount:输入设定为双精度时,小数点以后的位数
    /// IsEnterToTab:输入回来键时,是否变为输入tab键
    /// </summary>
    class TextBoxCustomized : TextBox
    {
        // 字符消息
        private const int WM_CHAR = 0x0102;
        // 上下文菜单"粘贴"消息
        private const int WM_PASTE = 0x0302;

        E_CUS_TYPE _cusType = E_CUS_TYPE.NONE;
        double _maxValue = 9999;
        bool _isFu = false;
        int _dotCount = 2;
        bool _isEnterToTab = false;


        //限制输入类型
        public enum E_CUS_TYPE
        {
            //无限制
            NONE,
            INT,
            DOUBLE,
            ENGLISH,
            ENGLIST_INT,
        }

        //限制输入类型
        public E_CUS_TYPE CusType
        {
            set
            {
                _cusType = value;
            }
            get
            {
                return _cusType;
            }
        }

        //数字最大值
        public double MaxValue
        {
            set
            {
                _maxValue = value;
            }
            get
            {
                return _maxValue;
            }
        }


        //可以输入负数
        public bool IsFu
        {
            set
            {
                _isFu = value;
            }
            get
            {
                return _isFu;
            }
        }

        //小数点后的位数
        public int DotCount
        {
            set
            {
                _dotCount = value;
            }
            get
            {
                return _dotCount;
            }
        }

        //enter 转为 父窗口的 tab
        public bool IsEnterToTab
        {
            set
            {
                _isEnterToTab = value;
            }
            get
            {
                return _isEnterToTab;
            }
        }


        public TextBoxCustomized()
        {
            //去掉菜单
            if (ContextMenu == null)
            {
                ContextMenu = new ContextMenu();
            }
        }

        //按键
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            //判断 keypress 是否符合要求
            string text = this.Text;
            
            if (IsCheck(e.KeyChar, this.SelectionStart, this.Text, _maxValue))
            {
                base.OnKeyPress(e);
            }
            else
            {
                e.Handled = true;
            }
        }

        //键被按下 delete 转为 backspace
        protected override void OnKeyDown(KeyEventArgs e)
        {
            //delete 转为 backspace
            if (e.KeyCode == Keys.Delete)
            {
                if (this.SelectionStart < this.Text.Count())
                {
                    if (IsCheck('\b', this.SelectionStart + 1, this.Text, _maxValue))
                    {
                        return;
                    }
                }

                e.Handled = true;
            }

            //enter 转为 父窗口的 tab 认为当前窗口为活动窗口
            if (_isEnterToTab && e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
                return;
            }

            base.OnKeyDown(e);
        }

        //control + V 过虑
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys)Shortcut.CtrlV)
            {
                string text = Clipboard.GetText();

                foreach (var key in text)
                {
                    // 通过消息模拟键盘输入, SendKeys.Send()静态方法不行
                    SendCharKey(key);
                }
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        protected override void WndProc(ref Message m)
        {
            // 选择上下文菜单的"粘贴"操作
            if (m.Msg == WM_PASTE)  
            {
                // 模拟键盘输入
                SendKeys.Send(Clipboard.GetText());
            }
            else
            {
                base.WndProc(ref m);
            }

        }

        // 通过消息模拟键盘录入
        private void SendCharKey(char c)  
        {
            Message msg = new Message();

            msg.HWnd = this.Handle;
            // 输入键盘字符消息WM_CHAR
            msg.Msg = WM_CHAR;  
            msg.WParam = (IntPtr)c;
            msg.LParam = IntPtr.Zero;

            base.WndProc(ref msg);
        }


        private bool IsCheck(char key, int postion, string text, double maxValue)
        {
            switch (_cusType)
            {
                case E_CUS_TYPE.INT:
                    return IsInt(key, postion, text, maxValue, _isFu);
                case E_CUS_TYPE.DOUBLE:
                    return IsDouble(key, postion, text, maxValue, _isFu, _dotCount);
                case E_CUS_TYPE.ENGLISH:
                    return IsEnglish(key);
                case E_CUS_TYPE.ENGLIST_INT:
                    return IsEnglishInt(key);
                default:
                    break;
            }

            return true;
        }

        //录入的是数字
        private bool IsInt(char key, int postion, string text, double maxValue, bool isFu)
        {
            //删除 可以
            if ('\b' == key)
            {
                return true;
            }

            //第一位为负
            if (isFu && key == '-' && postion == 0)
            {
                return true;
            }
            //负号前不能有数字
            if (text.Length >= 1 && text[0] == '-' && postion == 0)
            {
                return false;
            }


            //去掉负号,两个字符以上,第一个不能为0
            //输入的数据为0
            if (text.Length >= 1 && (key == '0'))
            {
                //有负号
                if (text[0] == '-')
                {
                    if (text.Length >= 2)
                    {
                        //第二个不能为“0”
                        if (postion == 1)
                        {
                            return false;
                        }

                        //第三个为0时,第一个不能是0
                        if (postion == 2 && text[1] == '0')
                        {
                            return false;
                        }
                    }
                }
                //无负号
                else
                {
                    //第一个不能为“0”
                    if (postion == 0)
                    {
                        return false;
                    }

                    //第二个为0时,第一个不能是0
                    if (postion == 1 && text[0] == '0')
                    {
                        return false;
                    }
                }
            }

            if (Char.IsNumber(key))
            {
                string temp = text;
                temp = temp.Insert(postion, key.ToString());
                //不能大于maxValue
                if (Convert.ToDouble(temp) > maxValue)
                {
                    return false;
                }
                return true;
            }

            return false;
        }


        //录入的是浮点数 是否通过输入限制
        static public bool IsDouble(char key, int postion, string text, double maxValue, bool isFu, int dotCount)
        {
            //删除 可以
            if ('\b' == key)
            {
                StringBuilder temp = new StringBuilder(text);

                //内容为空可以
                if (temp.ToString().Count() == 0)
                {
                    return true;
                }

                temp.Remove(postion - 1, 1);

                //去掉内容后,内容为空可以
                if (temp.ToString().Count() == 0)
                {
                    return true;
                }
                //去掉后内容为“-”,可以
                if (temp.ToString() == "-")
                {
                    return true;
                }

                //不能大于maxValue
                if (Convert.ToDouble(temp.ToString()) > maxValue)
                {
                    return false;
                }

                return true;
            }

            //小数点后的位数
            string tempDot = text;
            tempDot = tempDot.Insert(postion, key.ToString());
            var dotArr = tempDot.Split(new char[] { '.' });
            if (dotArr.Count() == 2 && dotArr[1].Count() > dotCount)
            {
                return false;
            }

            //第一位为负
            if (isFu && key == '-' && postion == 0)
            {
                return true;
            }
            //负号前不能有数字
            if (text.Length >= 1 && text[0] == '-' && postion == 0)
            {
                return false;
            }


            //仅能有一个小数点 且不在第一位
            if (key == '.' && postion != 0)
            {
                //已有一个“.” 不可以,“.”前不能是“-”
                if (text.Contains(".") || text[postion - 1] == '-')
                {
                    return false;
                }
                else
                {
                    return true;
                }                
            }
            //去掉负号,两个字符以上,第一个不能为0
            //输入的数据为0
            if (text.Length >= 1 && (key == '0'))
            {
                //有负号
                if (text[0] == '-')
                {
                    if (text.Length >= 2)
                    {
                        //第二个不能为“0”
                        if (postion == 1)
                        {
                            return false;
                        }

                        //第三个为0时,第一个不能是0
                        if (postion == 2 && text[1] == '0')
                        {
                            return false;
                        }
                    }
                }
                //无负号
                else
                {
                    //第一个不能为“0”
                    if (postion == 0)
                    {
                        return false;
                    }

                    //第二个为0时,第一个不能是0
                    if (postion == 1 && text[0] == '0')
                    {
                        return false;
                    }
                }
            }

            //数字 可以
            if (Char.IsNumber(key))
            {
                string temp = text;
                temp = temp.Insert(postion, key.ToString());
                //不能大于maxValue
                if (Convert.ToDouble(temp) > maxValue)
                {
                    return false;
                }

                return true;
            }

            return false;
        }


        //录入的是英文
        private bool IsEnglish(char key)
        {
            //删除 可以
            if ('\b' == key)
            {
                return true;
            }

            //英文 可以
            if (Char.IsLetter(key))
            {
                return true;
            }

            return false;
        }


        //录入的是英文或数字
        private bool IsEnglishInt(char key)
        {
            //删除 可以
            if ('\b' == key)
            {
                return true;
            }

            //英文或数字可以
            if (Char.IsLetter(key) || Char.IsNumber(key))
            {
                return true;
            }

            return false;
        }
    }
}


原创粉丝点击