gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:44:38 26 4
gpt4 key购买 nike

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

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

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

关于validation - 如何将一个错误提供程序应用于所有空文本框 VB.net 2010,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28642446/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com