VB.Net - How do I prevent a text box changed event firing unless the user presses TAB (or ENTER)

Posted on 2011-05-09
Last Modified: 2012-05-11
I want to be able to error check the value of a textbox (could be decimal, float, text or integer).  At the moment as soon as the user begins to type (anything) into the text box, the change event fires and various msgboxes appear depending upon whether the text input was numeric or not.  

Is ther a way of switching off the event until the user presses the Enter or TAB key to indicate they are ready to move on to the next field?
I suspect something like: if keydown = TAB or ENTER, then error check, otherwise ignore.

Any ideas, please?
Question by:keithedwardb
    LVL 53

    Accepted Solution

    I think that you should move your validation to the textBox_keyPress event

    Private Sub keypressed(ByVal o As [Object], ByVal e As KeyPressEventArgs)
            ' The keypressed method uses the KeyChar property to check
            ' whether the ENTER key is pressed.

            ' If the ENTER key is pressed, the Handled property is set to true,
            ' to indicate the event is handled.

            If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
                e.Handled = True
            End If
        End Sub 'keypressed

    LVL 17

    Expert Comment

    use the Key_Up eventof the text box to handleONLY the keys you want
    LVL 15

    Assisted Solution

    Are you using DataBinding ?

      For numeric and Date values on DataBinding, you need Use DataSourceUpdateMode.OnValidation

    If you are´nt using DataBinding, then Use Validating event.
    This occurs before control lost focus (keyboard, mouse).
    If you set e.cancel= true on that trigger, then control do´nt lost focus.

    You can use the keyPress (return) as an aditional validation point, but remember that user can exit from field using Tab and also mouse.

    Author Closing Comment

    Hi Guys,
    Thanks for this.  I used both the keypress and the validating to error trap and this now works.  I daresay the code can be refined and streamlined in due course but it gets me out of a hole.
    Thanks again.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now