vb.net 如何检查 Masked 文本框是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17864949/
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
How to check if Masked textbox is empty?
提问by David
I have several textboxes and masked texboxes in a winform that I need to check if they are empty, null or nothing before proceeding.
我在一个 winform 中有几个文本框和蒙版文本框,我需要在继续之前检查它们是空的、空的还是什么都没有。
The code I have for the most part is working as intended, if there is an empty texbox I get a message telling the user that the textbox is empty and it exits the sub, but for some reason that is not checking the masked textboxes.
我的大部分代码都按预期工作,如果有一个空的 texbox,我会收到一条消息,告诉用户该文本框为空并退出 sub,但由于某种原因,它没有检查屏蔽的文本框。
Maybe I'm wrong and it is checking them, but since they have the mask they're not considered as empty or null.
也许我错了,它正在检查它们,但是由于它们具有掩码,因此它们不被视为空或空。
Your help with checking if the masked texboxes are empty would be much appreciated.
您在检查蒙面文本框是否为空方面的帮助将不胜感激。
This is the code:
这是代码:
Private Sub btnCargarInformacion_Click(sender As System.Object, e As System.EventArgs) Handles btnCargar.Click
For Each myControl As Control In Me.GroupBox1.Controls
If TypeOf (myControl) Is TextBox Then
If myControl.Text.Equals(String.Empty) Then
MessageBox.Show(String.Format("Please Fill the following Textboxes: {0}", String.Join(",", myControl.Name)))
End If
If myControl.Text.Equals(String.Empty) Then
Exit Sub
End If
End If
Next
Dim PartePersonalTableApt As New PersonalObraDataSetTableAdapters.PartePersonalTableAdapter
Dim PersonalObTableApt As New PersonalObraDataSetTableAdapters.PersonalObTableAdapter
PartePersonalTableApt.ClearBeforeFill = True
PartePersonalTableApt.FillByFecha(PersonalObraDataSet.PartePersonal, txtDate.Text, txtDepartamento.Text, txtTurno.Text)
PersonalObTableApt.ClearBeforeFill = True
PersonalObTableApt.Fillby(PersonalObraDataSet.PersonalOb)
End Sub
采纳答案by Tim
if textbox.MaskCompleted=True Then
'they entered something
else
' they didnt enter anything
Endif
回答by Steven Doggart
The problem is that you are only looking for TextBoxobjects in this line:
问题是您只TextBox在此行中查找对象:
If TypeOf (myControl) Is TextBox Then
Since the MaskedTextBoxcontrol does not inherit from the TextBoxclass, you would need to check for that type separately, like this:
由于MaskedTextBox控件不是从TextBox类继承的,因此您需要单独检查该类型,如下所示:
If (TypeOf (myControl) Is TextBox) Or (TypeOf (myControl) Is MaskedTextBox) Then
However, since they do both inherit from the TextBoxBaseclass, you could just check for that instead:
但是,由于它们都继承自TextBoxBase该类,因此您可以改为检查它:
If TypeOf (myControl) Is TextBoxBase Then
回答by Eric J
Try this:
尝试这个:
If TypeOf myControl Is MaskedTextBox Then
If CType(myControl, MaskedTextBox).Text.Equals(String.Empty) Then
MessageBox.Show(String.Format("Please Fill the following Textboxes: {0}", String.Join(",", myControl.Name)))
End If
If CType(myControl, MaskedTextBox).Text.Equals(String.Empty) Then
Exit Sub
End If
End If
回答by keyboardP
Untested but instead of checking against string.empty, you could check it against the MaskedTextBox's Maskproperty.
未经测试,但string.empty您可以根据 MaskedTextBox 的Mask属性检查它,而不是检查它。
If myControl.Text.Equals(myControl.Mask) Then
MessageBox.Show(String.Format("Please Fill the following Textboxes: {0}", String.Join(",", myControl.Name)))
End If

