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.
Asked
Active
Viewed 358 times
-3
-
Duplicate of [How do I make a textbox that only accepts numbers?](http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers), [How to Force only numeric values be allowed in maskedtextbox?](http://stackoverflow.com/questions/10758683/how-to-force-only-numeric-values-be-allowed-in-maskedtextbox) and many others. Please use the search. :-) – CodeCaster Dec 21 '13 at 17:12
-
I have seen that question before. it used the if instruction. I wanna do it by masking. – sesideh Dec 21 '13 at 17:16
-
There is nothing wrong with `if`, but for masking see see the second link. Please show some effort from your side. :-) – CodeCaster Dec 21 '13 at 17:21
-
I have to say that if you use if instruction when you have more than 5 or 6 forms it takes a lot of time that your program perform its task. – sesideh Dec 21 '13 at 17:23
-
That does not seem to make any sense. An few `if`'s won't be considerably slower or faster than a masked textbox, they both evaluate in at most a few miliseconds. – CodeCaster Dec 21 '13 at 17:25
-
but it does have different. any way thanks for your help. i will show more effort. – sesideh Dec 21 '13 at 17:29
3 Answers
0
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;
}

Sudhakar Tillapudi
- 25,935
- 5
- 37
- 67
-
I don't want if instruction. it isn't useful when you have more than 5 or 6 form – sesideh Dec 21 '13 at 17:19
0
How to Add MaskedTextBox in .NET
Steps
Click on Toolbox in the left-side Bar Navigate and Find MaskedTextBox .NET Component Double click on it to get it on Windows Form
Steps
Click on Properties in Right-sidebar. It appears as shown in the image below Click on Set Mask or directly click on Browse button A dialog box appears put value according to your requirement in Mask labeled Textbox Click Ok

sesideh
- 17
- 5