[英]Check for empty TextBox controls in VB.NET
我在VB.NET中有一個Form應用程序。
我在一個表格上有很多文本框(大約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控件的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
一種非常簡單的方法是使用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
如果TextBox
字段為空,則會出現消息框,顯示“Complete Entry!”。
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
Sub用於檢查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
我找到了這個,也許你可以修改它來檢查所有的文本框是否清晰而不是它當前所做的只是清除所有文本框
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
結束班
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.