我有这段代码,使用过多的错误提供程序分配给每个文本框。还有其他方法(或更短的方法),特别是为所有文本框设置一个错误提供程序吗?
If txtHolidayName.Text = "" Then
ErrorProvider2.Clear()
ErrorProvider3.Clear()
ErrorProvider1.SetError(txtHolidayName, "Holiday name is required!")
txtHolidayName.Focus()
ElseIf dtpHoliday.Value < Date.Today And txtHolidayName.Text <> "" Then
ErrorProvider1.Clear()
ErrorProvider3.Clear()
ErrorProvider2.SetError(dtpHoliday, "Holiday date must be greater than the current date!")
dtpHoliday.Focus()
ElseIf txtHolidaySalary.Text = "" And dtpHoliday.Value > Date.Today Then
ErrorProvider1.Clear()
ErrorProvider2.Clear()
ErrorProvider3.SetError(txtHolidaySalary, "Holiday salary is required!")
txtHolidaySalary.Focus()
最佳答案
看一下这个....
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
在私有(private)子部分中,将textbox2.validating更改为您的控件名称。然后使用它来调用此函数。
ErrorProvider1.Clear()
If Me.ValidateChildren()
' continue on
End If
我没做约翰·科纳(John Koerner)做到了。这是链接
Check for empty TextBox controls in VB.NET