我想限制可以在文本框中輸入什麼數字和字母。假設我只想讓數字0-5和字母a-d(大寫和小寫)。 我已經嘗試過使用蒙面文本框,但它只允許我指定數字,僅限字母(不限制)或數字和字母,但按特定順序排列。 最佳方案是:用戶嘗試輸入數字6,並且沒有任何內容輸入到文本框中,對於範圍a-f之外的字母也是如此。 我認爲使用最好的事件是Keypress事件,但我對如何實現限制事項感到不知所措。限制文本框中的數字和字母 - C#
回答
// 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;
}
}
這工作就像一個魅力!我只需要添加對這些字母的限制。現在我想到了,如果我擁有同樣的形式的3個文本框,並且我想檢查所有這些文本框,那麼做同樣的事情的最佳方式是什麼?我正在考慮將所有事情都納入一種方法,但後來我意識到有些事件屬於特定的控制。我可以爲每個文本框複製相同的代碼,但它會是很多代碼!你會怎麼做? – gacanepa 2012-03-07 13:58:34
使用KeyDown事件,如果e.Key
是不是在你的允許設定,然後就e.Handled = true
。
另一種方法是接受所有輸入,驗證它,然後向用戶提供有用的反饋,例如錯誤標籤要求他們在一定範圍內輸入數據。我更喜歡這種方法,因爲用戶知道出了什麼問題並且可以修復它。它在整個網絡表單上使用,對於你的應用程序用戶來說並不令人意外。按下按鍵並根本得不到答案可能會讓人困惑!
http://en.wikipedia.org/wiki/Principle_of_least_astonishment
這與WinForms有關,對不起,我忘了那個細節。 – gacanepa 2012-03-07 13:07:04
沒問題 - 我爲你的問題添加了標籤。雖然良好的UI設計原則仍然適用! – 2012-03-07 13:09:11
按鍵事件可能是你最好的選擇。如果輸入的字符不是您想要的字符,請在那裏進行檢查,將e.SuppressKey
設置爲true
以確保KeyPress事件未觸發,並且字符未添加到文本框中。
將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;
}
覆蓋的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;
}
這會告訴該鍵將考慮作爲用戶輸入或不文本框。
如果您使用的是ASP.NET Web Forms,則正則表達式驗證將是最簡單的。在MVC中,像MaskedEdit這樣的jQuery庫將是一個很好的開始。上面的答案很好地記錄了Windows窗體的方法。
- 1. 文本框只有字母,數字和 -
- 2. 如何限制在字母數字文本框中輸入9位數字
- 3. 限制文字文本框
- 4. 在文本框中限制文字
- 5. 限制文本框中的字符
- 6. 文本框字符限制
- 7. 將文本框/文本框中的輸入限制爲僅在J2me中使用字母數字輸入
- 8. 將文本框輸入限制在C#中的數字#
- 9. 限制文本框中的字符
- 10. asp文本框限制字符數?
- 11. 僅限二進制數字文本框
- 12. 在數字文本框中限制指數和句點(。)html
- 13. 實施字母數字文本框
- 14. 限制文本框中的字符串或數字Java android
- 15. C#文本框限制輸入字符和快捷鍵
- 16. 文本字符數限制
- 17. 只驗證數字和字母只在我的文本框
- 18. android edittext限制文本和數字
- 19. 帶有字母數字檢查的文本框中的javascript
- 20. 文本框僅包含字母,數字和空格
- 21. 在數字文本框中限制字符串的複製粘貼
- 22. 字符限制在文本框
- 23. NSCharacterSet:如何將「_」添加到字母數字字符集文本限制?
- 24. 只允許文本框中的字母數字
- 25. 驗證文本框中的字母數字輸入[VB2010]
- 26. c#數字文本框
- 27. 如何驗證長度並將文本字段限制爲僅適用於iOS中的數字,字母數字和字母字符
- 28. 製作一串字母和數字?
- 29. 當第一個字母是字母數字時接受文本框的值
- 30. 如何創建只接受數字的文本框和只接受WPF中的字母的文本框?
不要忘記複製/粘貼... – ken2k 2012-03-07 13:00:43
這與WinForms有關嗎? – 2012-03-07 13:01:13
看到[這個問題可能會幫助你](http://stackoverflow.com/questions/828309/textbox-allow-only-letters)(適用於ASP.NET) – Shai 2012-03-07 13:01:40