我在 VB.NET 中有一个 Form 应用程序.
Ive got a Form application in VB.NET.
我在一个表单上有很多文本框(大约 20 个).无论如何要一次检查它们是否为空,而不是写出大量代码来单独检查每个,例如
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
If txt1.text = "" Or txt2.text="" Then
msgbox("Please fill in all boxes")
这似乎还有很长的路要走?
That just seems like a long way around it?
你也可以使用 LINQ:
You could also use 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
有趣的方法是Enumerable.OfType
查询语法相同(在 VB.NET 中更易读):
The same in query syntax(more readable in 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
这篇关于在 VB.NET 中检查空的 TextBox 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!