Only Numbers in TextBox(摘录)
事情虽然小,但还是值得保留
http://www.ganshani.com/2008/04/10/only-numbers-in-textbox/
Requirement: TextBox in C# should accept only numbers as input.
Solution:
Raise an event KeyPress and paste following line of code.
private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar))
e.Handled = true;
}
Here, e stands for the arguments of the function and e contains several attributes and methods like:
e.KeyChar : Character that has been pressed
e.Handled : Whether it is an acceptable character or not.