How do I create a text box that accepts only numbers by hiding?

advertisements

This question already has an answer here:

  • How do I make a textbox that only accepts numbers? 29 answers

I have got a text box in WinForms which is just for entering phone numbers. how can I do it by just masking it. I have used if instruction but i want to use the mask. actually my biggest problem was that I didn't know even where is the mask text box.


You can use Char.IsDigit() function to check the user input.

Try This:

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