在VB.NET中检查空的TextBox控件 [英] Check for empty TextBox controls in VB.NET

查看:232
本文介绍了在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")

这似乎还有很长的路要走?

That just seems like a long way around it?

推荐答案

也可以使用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控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆