如何将一个错误提供程序应用于所有空文本框VB.net 2010

时间:2015-02-21 05:24:20

标签: validation error-handling textbox string

我有这个代码块,使用分配给每个文本框的太多错误提供程序。有没有其他方式(或更短的方法)特别是为所有文本框设置一个错误提供程序?

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()

1 个答案:

答案 0 :(得分:0)

检查出来......

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

在私有子部分中 - 将textbox2.validating更改为您的控件名称。然后用它来调用这个函数....

ErrorProvider1.Clear()
If Me.ValidateChildren()
    ' continue on
End If

我没有这样做。约翰科尔纳做到了。这是链接 Check for empty TextBox controls in VB.NET