Winform数字输入文本框

1、目的
使文本框只输入数字(包括小数)
2、步骤
在控件库中添加自定义控件(窗体);
修改代码;
3、代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lib
{
    public partial class GTextBox : TextBox
    {
        public GTextBox()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        #region 自定义属性
        private int maxIntegerLength = 10;
        private int minIntegerLength = 0;
        private int maxDecimalLength = 4;
        private int minDecimalLength = 0;
        private int integerLength;
        private int decimalLength;
        #endregion

        #region 自定义属性
        /// <summary>
        /// 最大整数位
        /// </summary>
        public int MaxIntegerLength
        {
            get
            {
                return maxIntegerLength;
            }
            set
            {
                if (value >= 0 && value >= minIntegerLength)
                {
                    maxIntegerLength = value;
                }
                else
                {
                    throw new Exception("最大整数位数不应小于最小整数位数");
                }
            }
        }
        /// <summary>
        /// 最小整数位数
        /// </summary>
        public int MinIntegerLength
        {
            get
            {
                return minIntegerLength;
            }
            set
            {
                if (value >= 0 && value <= maxIntegerLength)
                {
                    minIntegerLength = value;
                }
                else
                {
                    throw new Exception("最小整数位数不应大于最大整数位数");
                }
            }
        }
        /// <summary>
        /// 最大小数位数
        /// </summary>
        public int MaxDecimalLength
        {
            get
            {
                return maxDecimalLength;
            }
            set
            {
                if (value >= 0 && value >= minDecimalLength)
                {
                    maxDecimalLength = value;
                }
                else
                {
                    throw new Exception("最大小数位数不应小于最小小数位数");
                }
            }
        }
        public int MinDecimalLength
        {
            get
            {
                return minDecimalLength;
            }
            set
            {
                if (value >= 0 && value <= maxDecimalLength)
                {
                    minDecimalLength = value;
                }
                else
                {
                    throw new Exception("最小小数位数不应大于最大小数位数");
                }
            }
        }
        #endregion

        #region 重写方法
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            int editIndex = SelectionStart;             //获取当前编辑位
            if (e.KeyChar == (char)Keys.Back) return;   //如果为退格键,则退出
            if (e.KeyChar.Equals('.') || Char.IsNumber(e.KeyChar))  //如果为小数点或数字
            {
                if (Text.IndexOf(".") > -1)             //如果存在小数点
                {
                    if (e.KeyChar.Equals('.'))          //禁止重复输入小数点
                    {
                        e.Handled = true;
                        return;
                    }
                    else                                //如果为数字
                    {
                        if (SelectedText.Length > 0)     //禁止选中内容
                        {
                            return;
                        }
                        integerLength = Text.IndexOf(".");
                        decimalLength = Text.Length - integerLength - 1;
                        //控制最大小数位数
                        if (decimalLength >= maxDecimalLength && editIndex > Text.IndexOf("."))
                        {
                            e.Handled = true;
                            return;
                        }
                        //控制最大整数位数
                        if (integerLength > maxIntegerLength && editIndex <= Text.IndexOf("."))
                        {
                            e.Handled = true;
                            return;
                        }
                    }
                }
                else                     //如果不存在小数点
                {
                    //控制最大整数位数
                    integerLength = Text.Length;
                    if (integerLength == maxIntegerLength && !e.KeyChar.Equals('.'))
                    {
                        e.Handled = true;
                    }
                }
            }
            else                        //如果为非数字、非小数点
            {
                e.Handled = true;
            }
            base.OnKeyPress(e);
        }

        protected override void OnLeave(EventArgs e)
        {
            if (Text == null || Text == "") return; //空返回
            Text.TrimStart('0');            //移除前导的0
            if (Text.IndexOf(".") == -1)    //纯整数
            {
                integerLength = Text.Length;
                decimalLength = 0;
            }
            else                            //整数和小数
            {
                integerLength = Text.IndexOf(".");
                decimalLength = Text.Length - integerLength - 1;
                //验证小数位是否符合最小值(不足补0)
                if (decimalLength < minDecimalLength)
                {
                    Text = Text.PadRight(integerLength + minDecimalLength + 1, '0');
                }
            }
            //整数未输自动补零
            if (integerLength == 0)
            {
                Text = "0" + Text;
            }
            //验证整数位是否符合最小值
            if (integerLength < minIntegerLength)
            {
                Focus();
                Select(0, integerLength);
            }
            //验证整数位是否符合最大值
            if (integerLength > maxIntegerLength)
            {
                Focus();
                Select(0, integerLength);
            }
            base.OnLeave(e);
        }
        #endregion
    }
}
  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大浪淘沙胡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值