Win Forms text box masks

27,011

Solution 1

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.

Solution 2

You can use the MaskedTextBox control

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

Share:
27,011
eomeroff
Author by

eomeroff

please delete me

Updated on July 11, 2022

Comments

  • eomeroff
    eomeroff almost 2 years

    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.