less than 1 minute read

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.