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:

  • Step 1: Create a windows form as shown in the below image:

    Visual Studio -> File -> New -> Project -> WindowsFormApp



  • Step 2: Next, drag and drop the MaskedTextBox control from the toolbox on the form. As shown in the below image:

  • Step 3: After drag and drop you will go to the properties of the MaskedTextBox and set the value of the PromptChar property of MaskedTextBox control as shown in the below image:

    Output:

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:

  • Step 1: Create a MaskedTextBox using the MaskedTextBox() constructor is provided by the MaskedTextBox class.
    // Creating a MaskedTextBox
    MaskedTextBox m = new MaskedTextBox();
    
  • Step 2: After creating MaskedTextBox, set the PromptChar property of the MaskedTextBox provided by the MaskedTextBox class.
    // Setting the Prompt Character
    m.PromptChar = '-';
    
  • Step 3: And last add this MaskedTextBox control to the form using the following statement:
    // Adding MaskedTextBox control on the form
    this.Controls.Add(m);
    

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp39 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating and setting the
            // properties of the Label
            Label l1 = new Label();
            l1.Location = new Point(413, 98);
            l1.Size = new Size(176, 20);
            l1.Text = " Example";
            l1.Font = new Font("Bell MT", 12);
      
            // Adding label on the form
            this.Controls.Add(l1);
      
            // Creating and setting
            // the properties of Label
            Label l2 = new Label();
            l2.Location = new Point(242, 135);
            l2.Size = new Size(126, 20);
            l2.Text = "Phone number:";
            l2.Font = new Font("Bell MT", 12);
      
            // Adding label on the form
            this.Controls.Add(l2);
      
            // Creating and setting the
            // properties of MaskedTextBox
            MaskedTextBox m = new MaskedTextBox();
            m.Location = new Point(374, 137);
            m.Mask = "000000000";
            m.Size = new Size(176, 20);
            m.Name = "MyBox";
            m.BorderStyle = BorderStyle.Fixed3D;
            m.PromptChar = '-';
            m.Font = new Font("Bell MT", 18);
      
            // Adding MaskedTextBox 
            // control on the form
            this.Controls.Add(m);
        }
    }
    }

    chevron_right

    
    

    Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.