我在VB.NET中有一个Form应用程序。
我在一张表格上有很多文字框(大约20张)。无论如何都要一次检查它们以查看它们是否为空而不是写出大量的代码来单独检查每一个,例如
If txt1.text = "" Or txt2.text="" Then
msgbox("Please fill in all boxes")
这似乎还有很长的路要走? p>
答案 0 :(得分:18)
你也可以使用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
答案 1 :(得分:7)
我建议使用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
然后你可以打电话:
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
答案 2 :(得分:5)
非常简单的方法是使用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
答案 3 :(得分:1)
如果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
答案 4 :(得分:1)
用于检查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
答案 5 :(得分:0)
我发现了这个,也许你可以修改它来检查所有文本框是否清晰而不是它当前所做的只是清除所有文本框
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
答案 6 :(得分:-1)
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
结束班