如何讓TextBox只能輸入數字、漢字、字母?

來源:互聯網
上載者:User

在編程的過程中我們可能經常會用到TextBox只接受數字輸入(或者其他,比如:漢字,字母,等等),這個時候我們可能需要重新封裝一個TextBox(其他方法當然也可以),經常看到有人問這個問題,今天抽了一點時間將此封裝做了一下,現在共用大家,希望能給大家帶來一定的協助,如有不妥敬請斧正,另外:如需轉載請註明出處:
半支煙阿傑。http://blog.csdn.net/gisfarmer/ 謝謝。
只能輸入數字(int型)的代碼(直接複製就OK了)

  1. public class IntTextBox : System.Windows.Forms.TextBox
  2.     {
  3.         private int selectPos = 0;
  4.         public IntTextBox()
  5.             : base()
  6.         {
  7.             this.BackColor = Color.Beige;
  8.             this.TextChanged += new EventHandler(this.TextChage);
  9.             this.Leave += new EventHandler(this.FocusLeave);
  10.         }
  11.         //焦點發生改變時,此處你可以根據自己需要自己選擇
  12.         public void FocusLeave(object sender, System.EventArgs e)
  13.         {
  14.             if (this.Text != "")
  15.             {
  16.                 this.Text = ToDBC(this.Text);
  17.                 if (!(new Regex(@"^-?/d+$")).IsMatch(this.Text))
  18.                 {
  19.                     this.BackColor = Color.OrangeRed;
  20.                     MessageBox.Show("輸入內容不合法!", "輸入提示");
  21.                     this.Focus();
  22.                 }
  23.                 else
  24.                 {
  25.                     this.BackColor = Color.Beige;
  26.                 }
  27.             }
  28.         }
  29.         //內容發生改變時
  30.         public void TextChage(object sender, System.EventArgs e)
  31.         {
  32.             selectPos = this.SelectionStart;
  33.             if (this.Text != "")
  34.             {   
  35.                 //此處採用鄭州運算式
  36.                 if (!(new Regex(@"^-?/d+$")).IsMatch(this.Text))
  37.                 {
  38.                     this.BackColor = Color.OrangeRed;
  39.                     this.SelectionStart = selectPos;
  40.                 }
  41.                 else
  42.                 {
  43.                     this.BackColor = Color.Beige;
  44.                     this.SelectionStart = selectPos;
  45.                 }
  46.             }
  47.             else
  48.             {
  49.                 this.BackColor = Color.Beige;
  50.             }
  51.         }
  52.         //全形轉換成半形,此功能也可以選擇
  53.         public string ToDBC(string input)
  54.         {
  55.             char[] c = input.ToCharArray();
  56.             for (int i = 0; i < c.Length; i++)
  57.             {
  58.                 if (c[i] == 12288)
  59.                 {
  60.                     c[i] = (char)32;
  61.                     continue;
  62.                 }
  63.                 if (c[i] > 65280 && c[i] < 65375)
  64.                     c[i] = (char)(c[i] - 65248);
  65.             }
  66.             return new string(c);
  67.         }
  68.     }

下面代碼是只能輸入浮點數(至於其他的比如漢字、字母等等原理一樣)

  1.     public class FloatTextBox : System.Windows.Forms.TextBox
  2.     {
  3.         private int selectPos = 0;
  4.         public FloatTextBox()
  5.             : base()
  6.         {
  7.             this.BackColor = Color.Beige;
  8.             this.TextChanged += new EventHandler(this.TextChage);
  9.             this.Leave += new EventHandler(this.FocusLeave);
  10.         }
  11.         //焦點發生改變時
  12.         public void FocusLeave(object sender, System.EventArgs e)
  13.         {
  14.             if (this.Text != "")
  15.             {
  16.                 this.Text = ToDBC(this.Text);
  17.                 if (!(new Regex(@"^/d+(/./d+)?$")).IsMatch(this.Text))
  18.                 {
  19.                     this.BackColor = Color.OrangeRed;
  20.                     MessageBox.Show("輸入內容不合法!", "輸入提示");
  21.                     this.Focus();
  22.                 }
  23.                 else
  24.                 {
  25.                     this.BackColor = Color.Beige;
  26.                 }
  27.             }
  28.         }
  29.         //內容發生改變時
  30.         public void TextChage(object sender, System.EventArgs e)
  31.         {
  32.             selectPos = this.SelectionStart;
  33.             if (this.Text != "")
  34.             {
  35.                 if (!(new Regex(@"^/d+(/./d+)?$")).IsMatch(this.Text))
  36.                 {
  37.                     this.BackColor = Color.OrangeRed;
  38.                     this.SelectionStart = selectPos;
  39.                 }
  40.                 else
  41.                 {
  42.                     this.BackColor = Color.Beige;
  43.                     this.SelectionStart = selectPos;
  44.                 }
  45.             }
  46.             else
  47.             {
  48.                 this.BackColor = Color.Beige;
  49.             }
  50.         }
  51.         //全形轉換成半形
  52.         public string ToDBC(string input)
  53.         {
  54.             char[] c = input.ToCharArray();
  55.             for (int i = 0; i < c.Length; i++)
  56.             {
  57.                 if (c[i] == 12288)
  58.                 {
  59.                     c[i] = (char)32;
  60.                     continue;
  61.                 }
  62.                 if (c[i] > 65280 && c[i] < 65375)
  63.                     c[i] = (char)(c[i] - 65248);
  64.             }
  65.             return new string(c);
  66.         }
  67.     }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.