一个只能输入数值型数据的文本框类实现(C#)

本文介绍了一个C#自定义控件TextBoxForDigit,该控件限制用户只能输入数值型数据。通过添加Validating事件处理和MouseHover事件处理,实现了输入验证和提示功能,确保输入的内容符合数值格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   

      在软件开发时,经常面临数值类型数据的输入,如和限定文本框只能输入数值型数据,是一件麻烦事情。开发一个只能输入数值型数据的文本框可以简化很多开发工作,其代码如下,供参考。

 

                  using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace PRisk
{
    class TextBoxForDigit:TextBox
    {
        public ToolTip toolTip=new ToolTip() ;
        public double max;
        public double min;
        public string tip = "输入数字型数据";
        public TextBoxForDigit()
        {
            this.Validating +=
                 new System.ComponentModel.CancelEventHandler(NumberValidating);
            this.MouseHover += new System.EventHandler(mMouseHover);
          
            toolTip.SetToolTip(this , tip);
       

        }
        private void NumberValidating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!CheckIfTextBoxNumeric((TextBox)sender))
            {
                // myLabel.Text = "Has to be numeric";
                e.Cancel = true;
            }
        }
        private void mMouseHover(object sender, EventArgs e)
        {
            toolTip.SetToolTip(this, "输入数字型数据");
        }


        private bool CheckIfTextBoxNumeric(TextBox textBox)
        {
            bool isValid = true;
            int pointCount = 0, negativeCount = 0;
            if (textBox.Text == "")//判断是否为空
            {
                isValid = false; return isValid;


            }
            char ch;
            for (int i = 0; i < textBox.Text.Length; i++)
            {
                ch = textBox.Text[i];

              
                if (!System.Char.IsDigit(ch))
                {
                    if (ch == '.') //如果是小数点
                    {
                        pointCount++;
                        if (i == 0 || i == textBox.Text.Length - 1)//如果在开始或结尾
                        {
                            textBox.Text = ""; isValid = false; return isValid;
                        }

                    }
                    if (ch == '-') //如果是负号
                        {
                            negativeCount++;
                            if (i!=0)//如果不在开始
                            {
                                textBox.Text = ""; isValid = false; return isValid;
                            }
                        }
                        if (ch != '-' && ch != '.')
                        { textBox.Text = ""; isValid = false; return isValid; }
                              
                }
            }
            if (negativeCount > 1 || pointCount > 1)
              { textBox.Text = ""; isValid = false; return isValid; }
            return isValid;

        }
   
                                   
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值