首页 > 其他 > 详细

限制输入的TextBox

时间:2015-12-29 12:22:08      阅读:194      评论:0      收藏:0      [点我收藏+]

由于业务需要,在开发WinForm应用程序时,需要校验用户输入的信息,也就是通常所说的防呆。

这样的做法有很多,比如在光标离开当前的控件时进行校验并提示(很多朋友以为在WinForm中没有光标离开事件,其实只是在属性窗口看不到),还有就是在提交的时候统一校验等。还有一种方法,就是限制用户的输入,比如在一个输入框中,需要输入的是数字,在输入字符后,则不予接纳。先来实现只能输入数字的输入框。

首先,我们新建一个类,继承自TextBox,命名为LimitedTextBox:

    public class LimitedTextBox:TextBox
    {

    }

继承后,LimitedTextBox可以像TextBox一样使用了,重新生成一下,就可以在工具箱中找到它。

技术分享

为了实现的限制输入,我们需要重写(override)OnKeyPress方法,并在方法中校验输入的信息:

 public class LimitedTextBox : TextBox
    {
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);

            if (!char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }

        }
    }

e.Handled表示已经处理过了。这样这个KeyPress就相当于已经完成了。不会再出现在输入框中。

根据上面的方法,举一反三,可以扩展成丰富的输入限制,如只能输入数字、字母、数字和字母、数字字母下划线、正整数、负整数、小数、大于0小于1的小数(百分比)等。

则在很多场合都能够使用。以下方法采用枚举的方法实现,也可以用接口注入的方式(便于扩展)。

    /// <summary>
    /// 防呆管控,防止非正常数据的输入
    /// </summary>
    public class LimitedTextBox : TextBox
    {
        private LimitedKeyInType keyInType = LimitedKeyInType.All;

        [Browsable(true)]
        [DefaultValue(LimitedKeyInType.All)]
        public LimitedKeyInType LimitedTextBoxType
        {
            get
            {
                return keyInType;
            }
            set
            {
                this.Text = "";
                keyInType = value;
            }
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            if (this.ReadOnly)
            {
                return;
            }
            if (e.KeyChar == (char)0x08)
            {
                return;
            }
            switch (LimitedTextBoxType)
            {
                case LimitedKeyInType.Int:
                    if (char.IsDigit(e.KeyChar) || e.KeyChar == -)
                    {
                        if (string.IsNullOrEmpty(this.Text) && e.KeyChar == 0)
                        {
                            e.Handled = true;
                        }
                        if ((!string.IsNullOrEmpty(this.Text)) && e.KeyChar == -)
                        {
                            e.Handled = true;
                        }
                    }
                    else
                    {
                        e.Handled = true;
                    }
                    return;
                case LimitedKeyInType.PositiveInt:
                    if (char.IsDigit(e.KeyChar))
                    {
                        if (string.IsNullOrEmpty(this.Text) && e.KeyChar == 0)
                        {
                            e.Handled = true;
                        }
                    }
                    else
                    {
                        e.Handled = true;
                    }
                    return;
                case LimitedKeyInType.NegativeInt:
                    if (char.IsDigit(e.KeyChar) || e.KeyChar == -)
                    {
                        if (string.IsNullOrEmpty(this.Text) && e.KeyChar != -)
                        {
                            e.Handled = true;
                        }
                        if (this.Text.Length == 1 && e.KeyChar == 0)
                        {
                            e.Handled = true;
                        }
                        if (e.KeyChar == - && (!string.IsNullOrEmpty(this.Text)))
                        {
                            e.Handled = true;
                        }
                    }
                    else
                    {
                        e.Handled = true;
                    }
                    return;
                case LimitedKeyInType.Decimal:
                    if (char.IsDigit(e.KeyChar) || e.KeyChar == . || e.KeyChar == -)
                    {
                        if (string.IsNullOrEmpty(this.Text) && (e.KeyChar == 0 || e.KeyChar == .))
                        {
                            e.Handled = true;
                        }
                        if (e.KeyChar == - && (!string.IsNullOrEmpty(this.Text)))
                        {
                            e.Handled = true;
                        }
                        if (e.KeyChar == . && this.Text.IndexOf(.) > 0)
                        {
                            e.Handled = true;
                        }
                        if ((this.Text == "0" || this.Text == "-0") && e.KeyChar != .)
                        {
                            e.Handled = true;
                        }
                    }
                    else
                    {
                        e.Handled = true;
                    }
                    return;
                case LimitedKeyInType.PositiveDecimal:
                    if (char.IsDigit(e.KeyChar) || e.KeyChar == .)
                    {
                        if (string.IsNullOrEmpty(this.Text) && (e.KeyChar == .))
                        {
                            e.Handled = true;
                        }
                        if (e.KeyChar == . && this.Text.IndexOf(.) > 0)
                        {
                            e.Handled = true;
                        }
                        if (this.Text == "0" && e.KeyChar != .)
                        {
                            e.Handled = true;
                        }
                    }
                    else
                    {
                        e.Handled = true;
                    }
                    return;
                case LimitedKeyInType.NegativeDecimal:
                    if (char.IsDigit(e.KeyChar) || e.KeyChar == . || e.KeyChar == -)
                    {
                        if (string.IsNullOrEmpty(this.Text) && e.KeyChar != -)
                        {
                            e.Handled = true;
                        }
                        if (this.Text.Length == 1 && e.KeyChar == .)
                        {
                            e.Handled = true;
                        }
                        if (e.KeyChar == - && (!string.IsNullOrEmpty(this.Text)))
                        {
                            e.Handled = true;
                        }
                        if (e.KeyChar == . && this.Text.IndexOf(.) > 0)
                        {
                            e.Handled = true;
                        }
                        if (this.Text == "-0" && e.KeyChar != .)
                        {
                            e.Handled = true;
                        }
                    }
                    else
                    {
                        e.Handled = true;
                    }
                    return;
                case LimitedKeyInType.BelowOneDecimal:
                    if (char.IsDigit(e.KeyChar) || e.KeyChar == .)
                    {
                        if (string.IsNullOrEmpty(this.Text) && e.KeyChar != 0)
                        {
                            e.Handled = true;
                        }
                        if (this.Text.Length == 1 && e.KeyChar != .)
                        {
                            e.Handled = true;
                        }
                        if (e.KeyChar == . && this.Text.IndexOf(.) > 0)
                        {
                            e.Handled = true;
                        }
                    }
                    else
                    {
                        e.Handled = true;
                    }
                    return;
                case LimitedKeyInType.BelowEqualsOneDecimal:
                    if (char.IsDigit(e.KeyChar) || e.KeyChar == .)
                    {
                        if (string.IsNullOrEmpty(this.Text) && (e.KeyChar != 1 && e.KeyChar != 0))
                        {
                            e.Handled = true;
                        }
                        if (this.Text == "1")
                        {
                            e.Handled = true;
                        }
                        else
                        {
                            if (e.KeyChar == . && this.Text.Length != 1)
                            {
                                e.Handled = true;
                            }
                            else if (this.Text.Length == 1 && e.KeyChar != .)
                            {
                                e.Handled = true;
                            }
                        }
                    }
                    else
                    {
                        e.Handled = true;
                    }
                    return;
                case LimitedKeyInType.Number:
                    if (!char.IsDigit(e.KeyChar))
                    {
                        e.Handled = true;
                    }
                    return;
                case LimitedKeyInType.Char:
                    if (!char.IsLetter(e.KeyChar))
                    {
                        e.Handled = true;
                    }
                    return;
                case LimitedKeyInType.CharNumber:
                    if (!char.IsLetterOrDigit(e.KeyChar))
                    {
                        e.Handled = true;
                    }
                    return;
                case LimitedKeyInType.CharNumberUnderline:
                    if (!char.IsLetterOrDigit(e.KeyChar) && e.KeyChar != _)
                    {
                        e.Handled = true;
                    }
                    return;
                default:
                    return;
            }

        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            if (LimitedTextBoxType.ToString().IndexOf("Decimal") >= 0)
            {
                if (this.Text != "")
                {
                    if (this.Text[this.Text.Length - 1] == .)
                    {
                        this.Text += 0;
                    }
                }
            }
        }

    }

    public enum LimitedKeyInType
    {
        /// <summary>
        /// 所有的字符
        /// </summary>
        All = 0,
        /// <summary>
        /// 整数
        /// </summary>
        Int,
        /// <summary>
        /// 正整数
        /// </summary>
        PositiveInt,
        /// <summary>
        /// 负整数
        /// </summary>
        NegativeInt,
        /// <summary>
        /// 小数
        /// </summary>
        Decimal,
        /// <summary>
        /// 正小数
        /// </summary>
        PositiveDecimal,
        /// <summary>
        /// 负小数
        /// </summary>
        NegativeDecimal,
        /// <summary>
        /// 大于0小于1的小数
        /// </summary>
        BelowOneDecimal,
        /// <summary>
        /// 大于0小于或等于1的小数
        /// </summary>
        BelowEqualsOneDecimal,
        /// <summary>
        /// 数字
        /// </summary>
        Number,
        /// <summary>
        /// 英文字符
        /// </summary>
        Char,
        /// <summary>
        /// 英文字符和数字
        /// </summary>
        CharNumber,
        /// <summary>
        /// 英文字符、数字和下划线
        /// </summary>
        CharNumberUnderline
    }

 技术分享

限制输入的TextBox

原文:http://www.cnblogs.com/xiebin2013/p/5085038.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!