【发布时间】:2011-07-21 07:20:00
【问题描述】:
我有一个复合用户控件,其中包含一个下拉“国家”控件和一个复选框。如果选中该复选框,我想显示一个带有工具提示消息的验证图标,通知用户需要选择国家/地区。
如果用户尝试保存更改,我想检查整个表单,包括这个复合用户控件,是否有错误,如果发现,取消保存。
我希望,在表单中,我能够调用 Me.Validate 函数,并且该函数将递归地检查表单上任何级别的任何控件,并返回一个指示是否存在错误的值。相反,该函数似乎触发了所有控件的验证事件(我想这没问题)并且无条件地返回 TRUE。 在复合 userControl 上调用 Validate 方法的行为也相同。
我是否必须编写自己的递归函数来检查此表单上的错误?
我包含我的代码是为了让人们也提供一般性建议。
Private Sub ComboOutOfCountry_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ComboOutOfCountry.Validating
ValidateComboOutOfCountry()
End Sub
Private Sub ValidateComboOutOfCountry()
If CheckOutOfCountry.Checked AndAlso _
(ComboOutOfCountry.Value Is Nothing OrElse ComboOutOfCountry.Value = DBCodeConstants.Omited) Then
ErrorProvider1.SetError(ComboOutOfCountry, "Country is required when ""Out of Country"" is selected")
Else
ErrorProvider1.SetError(ComboOutOfCountry, "")
End If
End Sub
Private Sub CheckOutOfCountry_CheckedChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckOutOfCountry.CheckedChanged
If Not CheckOutOfCountry.Checked Then
ErrorProvider1.SetError(ComboOutOfCountry, "")
End If
End Sub
Private Sub ComboOutOfCountry_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboOutOfCountry.ValueChanged
ValidateComboOutOfCountry() 'Clear error icon immediately if they selected a country
End Sub
【问题讨论】:
标签: winforms validation