在 VB.NET 中检查空的 TextBox 控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9489671/
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
Check for empty TextBox controls in VB.NET
提问by Dr.Pepper
Ive got a Form application in VB.NET.
我在 VB.NET 中有一个 Form 应用程序。
I have many text boxes on one form (about 20). Is there anyway to check them all at once to see if they are empty instead of writing out a massive line of code to check each one individually such as
我在一个表单上有很多文本框(大约 20 个)。无论如何要一次检查它们是否为空,而不是写出大量代码来单独检查每个,例如
If txt1.text = "" Or txt2.text="" Then
msgbox("Please fill in all boxes")
That just seems like a long way around it?
这似乎还有很长的路要走?
回答by Tim Schmelter
You could also use LINQ:
你也可以使用 LINQ:
Dim empty =
Me.Controls.OfType(Of TextBox)().Where(Function(txt) txt.Text.Length = 0)
If empty.Any Then
MessageBox.Show(String.Format("Please fill following textboxes: {0}",
String.Join(",", empty.Select(Function(txt) txt.Name))))
End If
The interesting method is Enumerable.OfType
有趣的方法是Enumerable.OfType
The same in query syntax(more readable in VB.NET):
查询语法相同(在 VB.NET 中更易读):
Dim emptyTextBoxes =
From txt In Me.Controls.OfType(Of TextBox)()
Where txt.Text.Length = 0
Select txt.Name
If emptyTextBoxes.Any Then
MessageBox.Show(String.Format("Please fill following textboxes: {0}",
String.Join(",", emptyTextBoxes)))
End If
回答by John Koerner
I would recommend using the Validating event of the TextBox controls, with an error provider control (just add one to your form):
我建议使用 TextBox 控件的 Validating 事件,以及一个错误提供程序控件(只需在表单中添加一个):
Private Sub TextBox_Validating( sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating, TextBox2.Validating, ComboBox1.Validating
Dim ctl As Control = CType(sender, Control)
If ctl.Text = ""
e.Cancel = True
ErrorProvider1.SetError(ctl,"Please enter a value")
End If
End Sub
Then you can just call:
然后你可以打电话:
ErrorProvider1.Clear()
If Me.ValidateChildren()
' continue on
End If
The nice thing about this is that the user is informed about which textbox is missing and required. This works with other controls besides textboxes, so you can provide a more complete solution. Also, if you get to a later point where one or two textboxes don't need to have values, you simply do not validate them instead of having to add special cases in your loops.
这样做的好处是,用户会被告知缺少和需要哪个文本框。这适用于除文本框之外的其他控件,因此您可以提供更完整的解决方案。此外,如果您稍后遇到一个或两个文本框不需要值的情况,您只需不验证它们,而不必在循环中添加特殊情况。
Finally, if you don't want to type out all of the controls, then you could do this in form load:
最后,如果您不想输入所有控件,那么您可以在表单加载中执行此操作:
For Each c As Control In Me.Controls
If TypeOf(c) is TextBox or TypeOf(c) is ComboBox
AddHandler c.Validating, AddressOf Me.TextBox_Validating
End If
Next
回答by Enrico Campidoglio
A very simplisticapproach would be to gather all the TextBoxcontrols in a sequence using the Enumerable.OfTypeLINQ method and then iterate through it in a For Eachloop:
一种非常简单的方法是TextBox使用Enumerable.OfTypeLINQ 方法按顺序收集所有控件,然后在For Each循环中对其进行迭代:
Dim textBoxes = Me.Controls.OfType(Of TextBox);
For Each t In textBoxes
If String.IsNullOrEmpty(t.Text) Then
MsgBox("...")
Exit For
End If
Next t
回答by Kristian Hernan C. Manuel
If TextBoxfield is empty then the message box will appear saying "Complete Entry!".
如果TextBox字段为空,则会出现消息框,提示“完成输入!”。
Dim t
For Each t In Me.Controls
If TypeOf t Is TextBox Then
If t.Text = "" Then
MsgBox("Complete Entry!")
Exit Sub
Exit For
End If
End If
Next
回答by Se Chan
Sub for check Empty Textbox in GroupBox, you can use this:
子检查GroupBox中的空文本框,您可以使用:
Public Sub CheckEmptyTextbox(Byval groupbox as GroupBox)
Dim txt as control
For Each txt in groupbox.Controls
IF TypeOF txt is Textbox then
IF txt.Text="" Then
MsgBox("Please Input data to textbox.")
Exit For
End IF
End IF
Loop
End Sub
回答by Brent Hacker
I found this, perhaps you can modify it to check if all textboxes are clear rather than what it currently does which is just clear all textboxes
我发现了这个,也许您可以修改它以检查所有文本框是否清晰,而不是它当前所做的只是清除所有文本框
Public Sub ClearTextBox(ByVal root As Control)
For Each ctrl As Control In root.Controls
ClearTextBox(ctrl)
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Text = String.Empty
End If
Next ctrl
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ClearTextBox(Me)
End Sub
回答by Samson Kinen Key
Public Class freestyle
公开课自由泳
Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
If Trim(TextBox3.Text) = "" And Me.Visible Then
MsgBox("fill in the textbox")
TextBox3.BackColor = Color.Yellow
Else
' MsgBox("great one !!!")
End If
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
If Trim(TextBox2.Text) = "" And Me.Visible Then
MsgBox("fill in the textbox")
TextBox2.BackColor = Color.Yellow
Else
'MsgBox("great one !!!")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Trim(TextBox1.Text) = "" Or Trim(TextBox2.Text) = "" Or Trim(TextBox3.Text) = "" And Me.Visible Then
MsgBox("Please fill the necesary", MsgBoxStyle.Critical, "Error")
TextBox1.Focus()
Else
MsgBox("Nice Work !!!")
End If
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
If Trim(TextBox1.Text) = "" And Me.Visible Then
MsgBox("fill in the textbox")
TextBox1.BackColor = Color.Yellow
Else
' MsgBox("great one !!!")
End If
End Sub
End Class
结束类

