if (!Char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar))
{
e.Handled = true;
base.OnKeyPress(e);
(例如Jonh空間詹布),但它的工作只是johnjambo如何在僅接受字符的文本框中輸入空格鍵?
if (!Char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar))
{
e.Handled = true;
base.OnKeyPress(e);
(例如Jonh空間詹布),但它的工作只是johnjambo如何在僅接受字符的文本框中輸入空格鍵?
只需加一個條件,它應該工作。
&& !char.IsWhiteSpace(e.KeyChar)
你的整個代碼看起來應該是這樣
if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) && !char.IsWhiteSpace(e.KeyChar))
{
e.Handled = true;
base.OnKeyPress(e);
}
爲IsWhiteSpace檢查添加過多的按鍵()
e.keychar可以在鑰匙被轉換對象是這樣的:
private void textBox_KeyPress_Event(object sender, KeyPressEventArgs e)
{
if (char.IsLetter(e.KeyChar) || (Keys)e.KeyChar == Keys.Space)
e.Handled = true;
}