我有一个VB.NET的表单应用程序。
在一个表单上有很多文本框(大约20个)。有没有办法一次性检查它们是否为空,而不是编写一长串代码逐个检查每个文本框,例如
If txt1.text = "" Or txt2.text="" Then
msgbox("Please fill in all boxes")
这看起来只是一个绕远路的方式?
您还可以使用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中更易读):
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
我建议使用TextBox控件的验证事件,并使用错误提供程序控件(只需将其添加到表单中):
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
ErrorProvider1.Clear()
If Me.ValidateChildren()
' continue on
End If
最后,如果您不想输入所有的控件,那么您可以在表单加载时这样做:
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
一个非常简单的方法是使用Enumerable.OfType LINQ 方法将所有的 TextBox
控件收集到序列中,然后在 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
检查 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
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
我找到了这个,也许你可以修改它来检查所有文本框是否为空,而不是像它当前所做的那样只清除所有文本框
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
公共类自由泳
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
结束类
Dim empty = Me.TabControl1.TabPages(1).Controls.OfType...
- Tim Schmelter