Prevent Ctrl+Tab from Selecting TabPages

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
2 posts Page 1 of 1
Contributors
User avatar
Rhez
Member
Member
Posts: 30
Joined: Sat Jan 04, 2014 7:24 am

I google this question but none of the result answer my problem.

somebody help me with preventing Ctrl+Tab from selecting TabPages ?
I used e.KeyCodes on KeyDown Events in TabControl and it is not working.
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

The way I found to fix this comes from this StackOverflow thread. The guy posting was trying to do something similar to you but in another .NET language, so I adapted it for VB.NET :)

Basically, you have to create a custom class that inherits TabControl, and then override the OnKeyDown sub so that you can ignore any keyboard input if the control, tab, shift or page up/down keys are pressed.

Here's the full code:
Code: Select all
Public Class CustomTabControl
    Inherits TabControl

    Protected Overrides Sub OnKeyDown(ke As KeyEventArgs)
        If (ke.KeyData = (Keys.Tab Or Keys.Control) Or ke.KeyData = (Keys.PageDown Or Keys.Control) Or ke.KeyData = (Keys.Tab Or Keys.Control Or Keys.Shift)) Then
            Return
        End If

        MyBase.OnKeyDown(ke)
    End Sub
End Class
You can place this code under the "End Class" of your Form1 class or whatever it's called. Also, you'll have to build your project once before CustomTabControl appears in your toolbox. Once that's done, you'll be able to add it to your project like any other control, and it will behave like any other TabControl except that it won't switch tabs when you press Ctrl+Tab cooll;
2 posts Page 1 of 1
Return to “Coding Help & Support”