Transfer from: http://hi.baidu.com/6phone/item/ca770c0f7c4b8f70bfe97e02
Assuming that the text box only allows the input of numbers, decimal point and backspace and enter keys, then:
method one:
Code for text box 1 (KeyPress event of TextBox1):
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{ //Prevent key input from the keyboard e.Handled = true;
//Do not block
if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar =='. when entering numbers from 0-9, decimal point, carriage return and backspace keys . '|| 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("Warning: You must enter a number!");
txtPrice.Focus();
txtPrice.SelectAll();
e.KeyChar = '\0';
}
}
}