Open In App

How to set the Prompt Character in MaskedTextBox in C#?

In C#, MaskedTextBox control gives a validation procedure for the user input on the form like date, phone numbers, etc. Or in other words, it is used to provide a mask which differentiates between proper and improper user input. In MaskedTextBox control, you are allowed to set a character that represents the absence of user input in the MaskedTextBox using PromptChar Property provided by the MaskedTextBox control. The default value of this property is an underscore(_). You can set this property in two different ways:

1. Design-Time: It is the easiest way to set the value of the PromptChar property of MaskedTextBox control as shown in the following steps:



2. Run-Time: It is a little bit trickier than the above method. In this method, you can set the prompt character for the MaskedTextBox control programmatically with the help of given syntax:

public char PromptChar { get; set; }

Here, Char represents the prompt character. If the value of the prompt character is similar to the password character, then it will throw an InvalidOperationException. And also throw an ArgumentException if the value of the prompt character is not valid which is determined by the IsValidPasswordChar(Char) method. The following steps show how to set the prompt character for the MaskedTextBox control dynamically:


Article Tags :
C#