views:

327

answers:

2

Hi everyone

How can I put mask on win form text box so that it allows only numbers? And how it works for another masks data, phone zip etc.

I am using Visual Studio 2008 C#

Thanks.

A: 

You can use the MaskedTextBox control

http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx

Aequitarum Custos
+1  A: 

Do you want to prevent input that isn't allowed or validate the input before it is possible to proceed?

The former could confuse users when they press keys but nothing happens. It is usually better to show their keypresses but display a warning that the input is currently invalid. It's probably also quite complicated to set up for masking an email-address regular expression for example.

Look at ErrorProvider to allow the user to type what they want but show warnings as they type.

For your first suggestion of a text box that only allows numbers, you might also want to consider a NumericUpDown.

Mark Byers
A properly designed user interface should leave no confusion as to why their text wasn't typed. I am on a development team doing Medical Billing software for people who have never used computers before, and even they get why letters won't go into a SSN or Insurance ID number field.
Aequitarum Custos
Mostly I would use validation of input, prevention just sometimes in cases like SSN and search by id etc.
SonOfOmer