2012-03-07 70 views
1

我想限制可以在文本框中輸入什麼數字和字母。假設我只想讓數字0-5和字母a-d(大寫和小寫)。 我已經嘗試過使用蒙面文本框,但它只允許我指定數字,僅限字母(不限制)或數字和字母,但按特定順序排列。 最佳方案是:用戶嘗試輸入數字6,並且沒有任何內容輸入到文本框中,對於範圍a-f之外的字母也是如此。 我認爲使用最好的事件是Keypress事件,但我對如何實現限制事項感到不知所措。限制文本框中的數字和字母 - C#

+8

不要忘記複製/粘貼... – ken2k 2012-03-07 13:00:43

+1

這與WinForms有關嗎? – 2012-03-07 13:01:13

+0

看到[這個問題可能會幫助你](http://stackoverflow.com/questions/828309/textbox-allow-only-letters)(適用於ASP.NET) – Shai 2012-03-07 13:01:40

回答

1
// Boolean flag used to determine when a character other than a number is entered. 
private bool nonNumberEntered = false; 

// Handle the KeyDown event to determine the type of character entered into the control. 
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) 
{ 
    // Initialize the flag to false. 
    nonNumberEntered = false; 

    // Determine whether the keystroke is a number from the top of the keyboard. 
    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) 
    { 
     // Determine whether the keystroke is a number from the keypad. 
     if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9) 
     { 
      // Determine whether the keystroke is a backspace. 
      if(e.KeyCode != Keys.Back) 
      { 
       // A non-numerical keystroke was pressed. 
       // Set the flag to true and evaluate in KeyPress event. 
       nonNumberEntered = true; 
      } 
     } 
    } 
    //If shift key was pressed, it's not a number. 
    if (Control.ModifierKeys == Keys.Shift) { 
     nonNumberEntered = true; 
    } 
} 

// This event occurs after the KeyDown event and can be used to prevent 
// characters from entering the control. 
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
{ 
    // Check for the flag being set in the KeyDown event. 
    if (nonNumberEntered == true) 
    { 
     // Stop the character from being entered into the control since it is non-numerical. 
     e.Handled = true; 
    } 
} 
+0

這工作就像一個魅力!我只需要添加對這些字母的限制。現在我想到了,如果我擁有同樣的形式的3個文本框,並且我想檢查所有這些文本框,那麼做同樣的事情的最佳方式是什麼?我正在考慮將所有事情都納入一種方法,但後來我意識到有些事件屬於特定的控制。我可以爲每個文本框複製相同的代碼,但它會是很多代碼!你會怎麼做? – gacanepa 2012-03-07 13:58:34

0

使用KeyDown事件,如果e.Key是不是在你的允許設定,然後就e.Handled = true

另一種方法是接受所有輸入,驗證它,然後向用戶提供有用的反饋,例如錯誤標籤要求他們在一定範圍內輸入數據。我更喜歡這種方法,因爲用戶知道出了什麼問題並且可以修復它。它在整個網絡表單上使用,對於你的應用程序用戶來說並不令人意外。按下按鍵並根本得不到答案可能會讓人困惑!

http://en.wikipedia.org/wiki/Principle_of_least_astonishment

+0

這與WinForms有關,對不起,我忘了那個細節。 – gacanepa 2012-03-07 13:07:04

+0

沒問題 - 我爲你的問題添加了標籤。雖然良好的UI設計原則仍然適用! – 2012-03-07 13:09:11

0

按鍵事件可能是你最好的選擇。如果輸入的字符不是您想要的字符,請在那裏進行檢查,將e.SuppressKey設置爲true以確保KeyPress事件未觸發,並且字符未添加到文本框中。

4

將KeyPress事件用於您的文本框。

protected void myTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs) 
{ 
    e.Handled = !IsValidCharacter(e.KeyChar); 
} 

private bool IsValidCharacter(char c) 
{ 
    bool isValid = true; 

    // put your logic here to define which characters are valid 
    return isValid; 
} 
1

覆蓋的PreviewKeyDownEvent這樣的:

private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 
    { 
     if (e.KeyCode == Keys.A || e.KeyCode == Keys.B || ...) 
      e.IsInputKey = true; 
     else 
      e.IsInputKey = false; 
    } 

這會告訴該鍵將考慮作爲用戶輸入或不文本框。

0

如果您使用的是ASP.NET Web Forms,則正則表達式驗證將是最簡單的。在MVC中,像MaskedEdit這樣的jQuery庫將是一個很好的開始。上面的答案很好地記錄了Windows窗體的方法。