WinForm 自定义控件 TextBoxNum只允许输入数字

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace CommonControl
{
    public enum ValType
    {
        Double,
        PositiveDouble,
        PositiveDoubleAndZero,
        Int,
        PositiveInt,
        PositiveIntAndZero
    }

    public partial class TextBoxNum : TextBox
    {
        #region 属性
        private ValType valueType = ValType.Double;

        [Description("值的类型"), Category("自定义")]
        public ValType ValueType
        {
            get { return valueType; }
            set { valueType = value; }
        }

        private double? maxValue;

        [Description("最大值"), Category("自定义")]
        public double? MaxValue
        {
            get { return maxValue; }
            set { maxValue = value; }
        }

        [Description("值,double类型,只读"), Category("自定义")]
        public double Value
        {
            get
            {
                double d;
                try
                {
                    d = Convert.ToDouble(this.Text);
                }
                catch
                {
                    d = 0;
                }
                return d;
            }
        }

        #endregion

        #region 构造方法
        public TextBoxNum()
        {
            InitializeComponent();
            RegEvent();
        }

        private void RegEvent()
        {
            this.LostFocus += TextBoxNum_LostFocus;
            this.KeyPress += TextBoxNum_KeyPress;
            this.TextChanged += TextBoxNum_TextChanged;
        }


        public TextBoxNum(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            RegEvent();
        }
        #endregion

        private void TextBoxNum_LostFocus(object sender, EventArgs e)
        {
            errorProvider1.SetError(this, "");
            if (this.Text == "")
            {
                return;
            }

            double d;

            try
            {
                switch (this.ValueType)
                {
                    case ValType.Double:
                    case ValType.PositiveDouble:
                    case ValType.PositiveDoubleAndZero:
                        d = Convert.ToDouble(this.Text);
                        break;
                    case ValType.Int:
                    case ValType.PositiveInt:
                    case ValType.PositiveIntAndZero:
                        d = Convert.ToInt32(this.Text);
                        break;
                    default:
                        d = 0;
                        break;
                }
            }
            catch
            {
                switch (this.ValueType)
                {
                    case ValType.Double:
                        errorProvider1.SetError(this, "必须是数字");
                        break;
                    case ValType.PositiveDouble:
                        errorProvider1.SetError(this, "必须是大于0的数字");
                        break;
                    case ValType.PositiveDoubleAndZero:
                        errorProvider1.SetError(this, "必须是大于等于0的数字");
                        break;
                    case ValType.Int:
                        errorProvider1.SetError(this, "必须是整数");
                        break;
                    case ValType.PositiveInt:
                        errorProvider1.SetError(this, "必须是大于0的整数");
                        break;
                    case ValType.PositiveIntAndZero:
                        errorProvider1.SetError(this, "必须是大于等于0的整数");
                        break;
                    default:
                        break;
                }
                this.SelectAll();
                this.Focus();
                return;
            }

            if (this.MaxValue != null)
            {
                if (d > this.MaxValue)
                {
                    this.Text = this.MaxValue.ToString();
                }
            }

            switch (this.ValueType)
            {
                case ValType.PositiveDouble:
                    if (d <= 0)
                    {
                        errorProvider1.SetError(this, "必须是大于0的数字");
                        this.SelectAll();
                        this.Focus();
                        return;
                    }
                    break;
                case ValType.PositiveDoubleAndZero:
                    if (d < 0)
                    {
                        errorProvider1.SetError(this, "必须是大于等于0的数字");
                        this.SelectAll();
                        this.Focus();
                        return;
                    }
                    break;
                case ValType.PositiveInt:
                    if (d <= 0)
                    {
                        errorProvider1.SetError(this, "必须是大于0的整数");
                        this.SelectAll();
                        this.Focus();
                        return;
                    }
                    break;
                case ValType.PositiveIntAndZero:
                    if (d < 0)
                    {
                        errorProvider1.SetError(this, "必须是大于等于0的整数");
                        this.SelectAll();
                        this.Focus();
                        return;
                    }
                    break;
            }
        }

        private void TextBoxNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '.') return;//允许输入 小数点 .
            if (e.KeyChar == '-') return;//允许输入 负号  -
            if (e.KeyChar == '\b') return;//允许输入 退格

            //数字0~9所对应的keychar为48~57
            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                e.Handled = true;//取消KeyPress事件
            }
        }

        private void TextBoxNum_TextChanged(object sender, EventArgs e)
        {
            errorProvider1.SetError(this, "");
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值