简体   繁体   English

创建蒙版文本框,但在为空时隐藏蒙版字符

[英]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.我试过使用MaskedTextBox ,但是当文本框为空时,它会在文本框中显示空的空行(下划线)。

How can I avoid that and show the masked textbox like a simple empty (still masked) textbox?我怎样才能避免这种情况并像一个简单的空(仍然被屏蔽)文本框一样显示被屏蔽的文本框?

Also, I want data like csc-(somenumber) .另外,我想要csc-(somenumber)类的数据。 Can I add some random number automatically after 'csc-' characters?我可以在“csc-”字符后自动添加一些随机数吗?

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.如果您希望提示在用户编辑文本时可见但在其他情况下隐藏,请将 HidePromptOnLeave 属性设置为 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 " ".如果您不想将下划线作为提示字符,您可以将 PromptChar 属性更改为空格“”。 You cannot make the PromptChar nothing, the field must have a value.你不能让 PromptChar 什么都没有,该字段必须有一个值。

MaskedTextBox1.PromptChar = " "

For your textbox, use the MaskedTextBox class.对于您的文本框,请使用 MaskedTextBox class。

http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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