[RESOLVED] KeyPress and Validation
Hello: I've been using KeyPress to help limit what the user enters into certain textboxes. e.g. here's my code for a Phone number. It only allows for numbers and dashes:
HTML Code:
Private Sub txtPhone_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPhone.KeyPress
If Not (Char.IsWhiteSpace(e.KeyChar) Or (Asc(e.KeyChar) = 45) Or Char.IsDigit(e.KeyChar) Or (Asc(e.KeyChar) = 8)) Then
'45 is dash 8 is backspace
lblMask.Visible = True
lblMask.Text = "Only numbers and dashes allowed."
e.Handled = True
Else
e.Handled = False
End If
lblValidate.Text = ""
End Sub
It's all working fine; however, the thing I hate about using it is that when ever have to do debugging of the screen, for each keypress into this textbox, it goes into debug.
I'm wondering if it's possible to do something similar when the user clicks the save button. Has anyone done any type of validation like this?
thanks for your inputs,
Proctor
Re: KeyPress and Validation
Use search on this forum to look for 'Numeric textbox' there are tons of them here. Everyone has written his own one, including myself (see my sig.)
Re: KeyPress and Validation
Thanks cicatrix: I'll be certain to run a search on that...but, in addition to that paticular senerio, I also have another text box that i only want to allow characters and periods. Do you have any suggestions for that?
thanks again,
Proctor
Re: KeyPress and Validation
I suggest you don't mess with the KeyPress and KeyDown. Use Validating event and validate the input there. There will be nothing wrong if a user enters something wrong in the text box. By using the Validating event you cann tell him that and ask to check the entered information.
Re: KeyPress and Validation
cicatrix: When you say to use the validation event....I've not used that ever. Is that used in conjunction with a certain control? e.g. text box validation control? I'm looking in my toolbox and I don't see any validation controls.
Thanks again,
Proctor
Re: KeyPress and Validation
Re: KeyPress and Validation
Thanks again for your help and for the link.
Proctor