简体   繁体   中英

Create masked textbox but hide masked characters when empty

I want to validate text taken from a textbox and display it in a specific format.

I have tried using a MaskedTextBox , but when the textbox is empty it shows the empty blank lines (underscores) in the text box.

How can I avoid that and show the masked textbox like a simple empty (still masked) textbox?

Also, I want data like csc-(somenumber) . Can I add some random number automatically after 'csc-' characters?

The reason the masked text box is showing a blank line is because underscore "_" is the default prompt character for a masked text box. You have two options for changing this.

If you want the prompt to be visible while the user is editing the text but hidden otherwise, set the HidePromptOnLeave property to true.

MaskedTextBox1.HidePromptOnLeave = True

If you never want to have the underscore as the prompt character, you can change the PromptChar property to be a space " ". You cannot make the PromptChar nothing, the field must have a value.

MaskedTextBox1.PromptChar = " "

For your textbox, use the MaskedTextBox class.

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

For getting a random number

Dim s = "csc-" & New Random().Next(1000, 10000).ToString

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM