检查屏蔽文本框是否为空 VB.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14233905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:50:44  来源:igfitidea点击:

Check Masked Text Box is empty or not VB.NET

vb.netmaskedtextbox

提问by Thanzeem

I have Masked Text Boxes in my form. One is for salary, PF&ESI and Other is for Phone Number. I try to check the Masked Text Box is empty or not with following code.

我的表单中有蒙版文本框。一个是薪水,PF&ESI,其他是电话号码。我尝试使用以下代码检查屏蔽文本框是否为空。

Dim mtxt As Control
Dim flag3 As Boolean
flag3 = False
For Each mtxt In EMPGBDATA.Controls
    If TypeOf mtxt Is MaskedTextBox  Then
        If mtxt.Text = "" Then
            mtxt.BackColor = Color.Red
            flag3 = True
        End If
    End If
Next 

Only my salary, PF&ESI Masked Text Box show in Red color, But Phone number Masked Text Box is not show Red.

只有我的薪水,PF&ESI 掩码文本框显示为红色,但电话号码掩码文本框不显示为红色。

回答by Steve

I think you have the following situation: (Probably defined by the property designer)

我认为你有以下情况:(可能是属性设计者定义的)

 maskedTextBoxPhoneNumber.Mask = "000000 00000"  'Or something similar'
 maskedTextBoxPhoneNumber.TextMaskFormat = MaskFormat.IncludeLiterals 

in this case your test for

在这种情况下,您的测试

 if mtxt.Text = "" then 

will fail because the literals included in the mask property are returned in the property Text

将失败,因为包含在掩码属性中的文字在属性中返回 Text

you should change the property TextMaskFormatto

您应该将属性更改TextMaskFormat

 maskedTextBoxPhoneNumber.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals

See MSDN for MaskFormat Enumeration

请参阅 MSDN 了解 MaskFormat 枚举