[C#]重写文本控件的OnKeyPress和OnTextChanged进行录入数据校验

该博客介绍了如何在C#中使用OnKeyPress和OnTextChanged事件来实现文本输入的数据校验。通过定义正则表达式过滤非法字符,并在OnTextChanged事件中检查数字格式,确保输入符合特定类型如Decimal4、Number或Int的要求。当输入超出限制时,代码会回滚到之前的合法状态。

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

//记录OnKeyPress前的字符
  string BeforKeyPressText = "";
  //记录OnKeyPress执行时输入被视为无效(e.Handled = true;)的次数 注:一个汉字和一个英文字母的输入各执行一次OnKeyPress
  int ErrKeyPressNum = 0;
  /// <summary>
  /// 实现输入控制:字符过滤
  /// by arming 2005-11-28
  /// </summary>
  /// <param name="e"></param>
  protected override void OnKeyPress(KeyPressEventArgs e)
  {
   //取出击键前的文本框文本
   BeforKeyPressText = this.Text;

   string[] regexExpressions=new string[2];
   regexExpressions= (string[])CommonDataDefine.HtDTRE()[this.InputType.ToString()];
   Regex regex = new Regex(regexExpressions[0]);
   Match m = regex.Match(e.KeyChar.ToString());
   if(!m.Success)
   {
    e.Handled = true;
    //被处理为无效输入
    ++ErrKeyPressNum;
   }

   base.OnKeyPress (e);
  }

  /// <summary>
  ///
  /// </summary>
  /// <param name="e"></param>
  protected override void OnTextChanged(EventArgs e)
  {
   if( ErrKeyPressNum > 1 )
   {
    --ErrKeyPressNum;
    return;
   }
   if( ErrKeyPressNum == 1 )
   {
    --ErrKeyPressNum;
    this.Text = BeforKeyPressText;
    this.Select(BeforKeyPressText.Length,0);
   }
   
   
   if(this.InputType.ToString()=="Decimal4"&&this.Text.Trim()!=""&&this.Text.Trim()!="-")
   {
    try
    {
     decimal inputDecimal = System.Convert.ToDecimal(this.Text.Trim());
     //数字长度
     int p = this.MaxLength;
     //小数点位置
     int d1 = BeforKeyPressText.IndexOf('.');
     int d2 = this.Text.Trim().IndexOf('.');
     //
     if(d2==-1&&this.Text.Trim().Length>p-4)
     {
      this.Text = BeforKeyPressText;
      this.Select(BeforKeyPressText.Length,0);
     }
     else if(d2!=-1&&(this.Text.Trim().Length-d2-1>4||d2>p-4))
     {
      this.Text = BeforKeyPressText;
      this.Select(BeforKeyPressText.Length,0);
     }
    }
    catch
    {
     this.Text = BeforKeyPressText;
     this.Select(BeforKeyPressText.Length,0);
    }
   }
   //整数 判断输入是否大于或小于Int所能保存的数值范围
   if((this.InputType.ToString()=="Number"||this.InputType.ToString()=="Int")&&this.Text.Trim()!=""&&this.Text.Trim()!="-")
   {
    try
    {
     int inputNum = System.Convert.ToInt32(this.Text.Trim());
    }
    catch
    {
     this.Text = BeforKeyPressText;
     this.Select(BeforKeyPressText.Length,0);
    }
   }
   //有两位小数点的小数 如Decimal(12,2)
   if(this.InputType.ToString()=="Decimal"&&this.Text.Trim()!=""&&this.Text.Trim()!="-")
   {
    try
    {
     decimal inputDecimal = System.Convert.ToDecimal(this.Text.Trim());
     //数字长度
     int p = this.MaxLength;
     //小数点位置
     int d1 = BeforKeyPressText.IndexOf('.');
     int d2 = this.Text.Trim().IndexOf('.');
     
     if(d2==-1&&this.Text.Trim().Length>p-2)
     {
      this.Text = BeforKeyPressText;
      this.Select(BeforKeyPressText.Length,0);
     }
     else if(d2!=-1&&(this.Text.Trim().Length-d2-1>2||d2>p-2))
     {
      this.Text = BeforKeyPressText;
      this.Select(BeforKeyPressText.Length,0);
     }
    }
    catch
    {
     this.Text = BeforKeyPressText;
     this.Select(BeforKeyPressText.Length,0);
    }
   }
   
   base.OnTextChanged (e);
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值