Validation - but from a class or function
I have numerous textboxes which I wish to validate and I am using
If IsNumeric(txtBossAnnualSalary.Text) =FalseThenErrorProvider1.SetError(txtBossAnnualSalary, _
"Please enter a numeric value")
ElseErrorProvider1.Dispose()
EndIfRather than typing this code multiple times in each validating event of each field I want to put it in a class or function then just call - ValidateMyTextBox(). I have tried this numerous times but can not quite get the validating error message to show in the field that called it.
Is this possible to do ?.
Thanks in advance
MJ
[1207 byte] By [
MarkJohn] at [2007-12-22]
from each validating event send the sender.name to the new method or even just use the one validating event and add multiple handlers eg
Private Sub textbox1_Validating(ByVal sender As Object, ByVal e As_
System.ComponentModel.CancelEventArgs) Handles textbox1.Validating,_
textbox2.Validating, textbox3.Validating
If IsNumeric(sender.text) = False Then
ErrorProvider1.SetError(sender.name, _
"Please enter a numeric value")
Else
ErrorProvider1.Dispose()
End If
End Sub
Thank you for the response but unfortunately I can not get the code to work as above.
Just to try it I created a TexBox1 and used the code below
Private
Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _Handles TextBox1.ValidatingIf IsNumeric(sender.text) = False ThenErrorProvider1.SetError(sender.name, "Please enter a numeric value")
ElseErrorProvider1.Dispose()
End If
End SubIt gives me a Sytem.invalidCastException, specified cast is not valid, any ideas ?
I think your errorprovider requires the object, not the object's name (i.e. ....SetError(Sender, "Please enter...) not ....SetError(Sender.Name, "Please....).