VB.NET Placeholder in MaskedTextBox

805 views Asked by At

I have a maskedtextbox with the following mask : 999999. I would like to add something like a placeholder in HTML to help the end user to know the mask of my maskedtextbox.

I think that i can create a specific mask when the form load and set the text property to "YYYYMM" (the placeholder that i need) and when the user enter in the control, reset the text and change the mask to 999999. However, i think there are better solutions.

I find some people talking about watermark control, but i would like to find something easier to use.

Any help will be appreciate. Thanks

2

There are 2 answers

0
nbadaud On BEST ANSWER

As suggested by tinstaafl in comments, i used a DateTimePicker instead of a MaskedTextBox.

I changed its properties :

CustomFormat = yyyMM
Format = Custom
ShowUpDown = True

This way, i have something similar to a textBox and i can select only the year or only the month and increase it with the up and down buttons.

4
David On

Use a DateTimePicker, that would be the easiest way, as suggested.

However, to do as the question asked...

On Form_Load, set MaskedTextBox1.Text = "YYYYMM"

Then, to get the effect of changing the mask to 999999 when the control is in focus, use the MaskedTextBox1_GotFocus method and use MaskedTextBox1.Text = "999999".