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

Posted on 2011-05-09
Medium Priority
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

Dhaest earned 1000 total points
ID: 35720826
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

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

Assisted Solution

x77 earned 1000 total points
ID: 35720965
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

ID: 35721453
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 Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline
Suggested Courses

807 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