Question TextBox validation problem

kekewong

Member
Joined
Mar 9, 2009
Messages
7
Programming Experience
Beginner
Hi , i am new to visual basic , hopefully this forum can help to solve this problem .

Below is my code:

Private Sub todayMonth_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles todayMonth.Validating
Dim tb As TextBox = DirectCast(sender, TextBox)
If Convert.ToInt32(tb.Text) >= 1 And Convert.ToInt32(tb.Text) <= 12 Then
tb.Tag = True
tb.BackColor = System.Drawing.SystemColors.Window

Else
tb.Tag = False
tb.BackColor = Color.Red
End If

End Sub

Actually, i wanted to validate the todayMonth textbox just to input number between 1 to 12 . The problem comes out when i do this procedure : Lets say i input an interger 11 , and it passes the validation and proceed to another textbox . Then i click back to the todayMonth textbox , it shows this error :

"Conversion from string "" to type 'Integer' is not valid."

I don't know why my value in the textbox is "null" or empty . I still saw the value 11 on the textbox . Can anyone explain why and how to solve it? Thanks .

Regards , kekewong
 
Use a NumericUpDown control where you allow value range 1-12.
 
Hello JohnH , is this the only way to solve this problem?
No, it is of course valid to write your own numeric input control as you are trying with the TextBox above, but I wouldn't do that in this case unless I had a good reason.
 
Back
Top