転送元:http://hi.baidu.com/6phone/item/ca770c0f7c4b8f70bfe97e02
テキストボックスで数字、小数点、バックスペースの入力のみが許可され、キーを入力できると仮定すると、次のようになります。
方法1:
テキストボックス1のコード(TextBox1のKeyPressイベント):
private void textBox1_KeyPress(object sender、KeyPressEventArgs e)
{ //キーボードからのキー入力を防止します e.Handled = true;
//ブロックしない
if(e.KeyChar> = '0' && e.KeyChar <= '9' || e.KeyChar == '。0〜9の数値、小数点、キャリッジリターン、バックスペースキーを入力する場合。 '|| e.KeyChar == 13 || e.KeyChar ==(char)8)
{ e.Handled = false; } }
private void textBox1_KeyPress(object sender、KeyPressEventArgs e)
{{
if(e.KeyChar <48 || e.KeyChar> 57)
{{
if(e.KeyChar!= 8 && e.KeyChar!= 13 && e.KeyChar!= 46)
{{
MessageBox.Show( "警告:番号を入力する必要があります!");
txtPrice.Focus();
txtPrice.SelectAll();
e.KeyChar = '\ 0';
}
}
}