⌘BABAKOV.NET Let's code!
100% [light] [ru]

Allowing only digits in TextBox in C#.

To allow only digits to be entered in a TextBox field in C#, the simplest way is to use a KeyPress event handler.

Here is how to do it:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsDigit(e.KeyChar)))
            {
                e.Handled = true;
            }
        }

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.