Create Validating Event Handler

TheRookie

Member
Joined
Sep 15, 2005
Messages
9
Programming Experience
Beginner
How do I create this for my input text boxes? Selecting the Class Name and Method Name is as far as I got.

If the input is not valid, I must set the foreground color of the text box to red. Otherwise, I have to set the foreground color to black.

Thanks,
 
Thanks Kulrom,

But now, how do I get it to distinguish between text and double. It needs to change the forecolor to red if text is entered, and change forecolor to black if the input is correct (double number).
 
In that case you have a wrong approch i beleive. If you want to be just sure that user has typed the number rather but text then i suppose it is best if you don't allow him/her to enter non-numerical values there ... Sounds simple isn't? And it is. Just put some code inside KeyDown or KeyPress (i prefer last one) event and check if pressed key isDigit i.e.
VB.NET:
[size=2][color=#0000ff]If [/color][/size][size=2][color=#0000ff]Char[/color][/size][size=2].IsDigit(e.KeyChar) [/size][size=2][color=#0000ff]OrElse _[/color][/size]
[size=2][color=#0000ff]Char[/color][/size][size=2].GetUnicodeCategory(e.KeyChar) = Globalization.UnicodeCategory.Control [/size][size=2][color=#0000ff][/color][/size]
[size=2][color=#0000ff]OrElse[/color][/size][size=2][color=#0000ff]Char[/color][/size][size=2].GetUnicodeCategory(e.KeyChar) = Globalization.UnicodeCategory.CurrencySymbol _
[/size][size=2][color=#0000ff]OrElse [/color][/size][size=2][color=#0000ff]Char[/color][/size][size=2].GetUnicodeCategory(e.KeyChar) = Globalization.UnicodeCategory.Format [/size][size=2][color=#0000ff]Then
[/color][/size][size=2]e.Handled = [/size][size=2][color=#0000ff]False[/color][/size]
[size=2][color=#0000ff]
[/color][/size][size=2][color=#0000ff]Else
 
[/color][/size][size=2]e.Handled = [/size][size=2][color=#0000ff]True
 
[/color][/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]If
[/color][/size]
Regards ;)
 
Last edited:
Back
Top