Check for empty TextBox controls in VB.NET(在 VB.NET 中检查空的 TextBox 控件)
问题描述
我在 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")
这似乎还有很长的路要走?p>
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 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 VB.NET 中检查空的 TextBox 控件


- 带问号的 nvarchar 列结果 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 使用 rss + c# 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01