Textbox to accept only numbers in C# windows Forms

By Santhosh N

This explains how to make C# windows Forms TextBox accept only numeric values.

In KeyPress event of the TextBox, writing the following code would prevent characters other than numbers to be entered in the TextBox.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            int num = 0;
             e.Handled = !int.TryParse(e.KeyChar.ToString(), out num);
        }

Related FAQs

This is how you can check if the given string is in numeric format or not in c#
Used to check if the column value is in numeric format
This is a way to check if the given string is in numeric format or not
Textbox to accept only numbers in C# windows Forms  (9170 Views)