enable/disable  tab function in form

Posted on 2009-05-01
Last Modified: 2013-11-26

I want to go thru a form with multiple textboxs and edit the fields as I go. Using the txtbox1.keydown and txtbox1.keypress I seem to be able yo captue the [Enter] key, validate the data and either move to the next txtbox2 if good or display an error message and re-focus on the current txtbox1.  The problem is these methods do not seem to trap the [Tab] key - which then bypasses my edits.  Using the following code I am able to capture the tab key, validate the data but I cannot suppress the key and re focus on the current txtbox...

Any way to not tab here?
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, _

                           ByVal keyData As Keys) As Boolean

        Const WM_KEYDOWN As Integer = &H100

        Const WM_SYSKEYDOWN As Integer = &H104

        If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then

            Select Case (keyData)

                Case Keys.Tab

                    'MsgBox("Tab Key Captured in the " + Me.ActiveControl.Name + " control!")

                    'I can do something based on the active control

                    Select Case Me.ActiveControl.Name

                          case TxtBox1

                               if not  validatedata1() then

                                   DON'T PROCESS TAB KEY!!!  ?????

                               end if

                    end select

Open in new window

Question by:norway77
    LVL 9

    Expert Comment

    DON'T PROCESS TAB KEY!!!  ????? = return true

    simple as that

    :) cheers
    LVL 15

    Accepted Solution

    How about using the Validating event for the control.

    If you don't like the input in the textbox, then just set the Cancel property in the CancelEventArgs to true. This prevents focus from being set to another control.

    Author Comment

    that worked great.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video discusses moving either the default database or any database to a new volume.

    737 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

    20 Experts available now in Live!

    Get 1:1 Help Now