Search code examples
c#maskedtextbox

C# display / (forward slash) in MaskedTextBox


I noticed that when the mask defined for input in a MaskedTextBox contains "/", it is automatically substituted by "-" in the textbox. I tried this using the default Date format available in VS, which in the form's Designer results in the following code

this.maskedTextBox2.Name = "maskedTextBox2";
this.maskedTextBox2.Mask = "00/00/0000";
this.maskedTextBox2.ValidatingType = typeof(System.DateTime);

and also for another MaskedTextBox by defining my custom mask in the form's constructor like this

InitializeComponent();
this.maskedTextBox1.Mask = @"00/00/0000";

In both cases the prompt displayed in the text boxes looks like this

__-__-____

Is there a way to actually display slashes there, instead of dashes? Marek


Solution

  • According to this page, a slash is a 'date separator' and therefore I'm guessing that your system currently runs in a locale where a date separator is a dash.

    / Date separator. The actual display character used will be the date symbol appropriate to the format provider, as determined by the control's FormatProvider property.

    If you really DO want a forward slash then "00\/00\/00" should do the trick (but then it's not really a compliant date input).