Validation - but from a class or function

I have numerous textboxes which I wish to validate and I am using

If IsNumeric(txtBossAnnualSalary.Text) =FalseThen

ErrorProvider1.SetError(txtBossAnnualSalary, _

"Please enter a numeric value")

Else

ErrorProvider1.Dispose()

EndIf

Rather 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]
# 1
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
BeanR at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

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.Validating

If IsNumeric(sender.text) = False Then

ErrorProvider1.SetError(sender.name, "Please enter a numeric value")

Else

ErrorProvider1.Dispose()

End If

End Sub

It gives me a Sytem.invalidCastException, specified cast is not valid, any ideas ?

MarkJohn at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
I think your errorprovider requires the object, not the object's name (i.e. ....SetError(Sender, "Please enter...) not ....SetError(Sender.Name, "Please....).
Richard_Wolf at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4
Thanks guys, that got it sorted now
MarkJohn at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

How do you remove the ! error icon from the screen after the error has been cleared?

bryndabella at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...