[C#]重寫文本控件的OnKeyPress和OnTextChanged進行錄入數據校驗

//記錄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);
  }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章