Results 1 to 7 of 7

Thread: [RESOLVED] KeyPress and Validation

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    513

    Resolved [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

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    513

    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

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    513

    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

  6. #6

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    513

    Re: KeyPress and Validation

    Thanks again for your help and for the link.

    Proctor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width