I want to validate the users input in a textbox by not accepting anything except integers ranging from 00 to 60.
Conditions:
- If the user input 0 or input nothing the system should considered it as 00 (take note that I padded it with 0.
- I know that I can do this also in
numericupdown, but for some reason I can't use that. - I need to apply this in 20 textboxes.
This is the code, actually it is working. My only problem is I can't apply it to 20 textboxes, it is only working in tb_hour_1 textbox, is there something that I can do instead of indication tb_hour_1.text in this line of code Dim num As Integer = Integer.Parse(String.Format("{0}{1}", If(Tb_Hour_1.Text = String.Empty, "", Tb_Hour_1.Text), e.KeyChar.ToString())).
Code:
Private Sub Tb_Hour_1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Tb_Minute_2.KeyPress, Tb_Minute_1.KeyPress,
Tb_Hour_2.KeyPress, Tb_Hour_1.KeyPress
Dim Min_Num As Integer = 0
Dim Max_Num As Integer = 60
If e.KeyChar <> ControlChars.Back Then
'allow backspace for deleting
e.Handled = Not Char.IsNumber(e.KeyChar)
'allow numbers only
If Not e.Handled Then
Dim num As Integer = Integer.Parse(String.Format("{0}{1}", If(Tb_Hour_1.Text = String.Empty, "", Tb_Hour_1.Text), e.KeyChar.ToString()))
If num < Min_Num OrElse num > Max_Num Then
e.Handled = True
End If
End If
End If
End Sub
Tb_Hour_1usesenderor a NumericUpDown