Disable Ctrl+Tab on MDI child forms

Is there a way to disable the CTRL-TAB key on all opened MDI child forms?

Is there a way of doing it w/o using the lost_focus and got_focus event of the forms?

thanks pple.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

turn on the KeyPreview property of the form, and write your code in key_down/key_press/key_up events.
In my version of VB, the MDI window does not handle keyboard events.

You can intercept the Windows WM_MDINEXT message if you subclass the MDI client window.  This message is sent to the window to activate the next or previous child window.  By intercepting and discarding this message, CTRL+TAB will not switch child windows.  Here is some sample code:

'MDI form code:
Option Explicit
Private hWndMDI As Long
Private bHooked As Boolean

Private Sub MDIForm_Load()
    bHooked = False
    ' Find the MDI client window
    hWndMDI = FindWindowEx(Me.hwnd, 0&, "MDIClient", vbNullString)
    If hWndMDI <> 0 Then
        ' Hook into the message stream.
        ' Important: Do not pause or add a breakpoint
        ' to the code while it is subclassed in IDE.
        glPrevWndProc = fSubClass(hWndMDI)
        bHooked = True
    End If
    ' Show the children
End Sub

Private Sub MDIForm_Unload(Cancel As Integer)
    If bHooked Then
        ' Stop intercepting messages.
        Call pUnSubClass(hWndMDI)
    End If
End Sub

'Module code
'Modified from a sample available at

Option Explicit

Public Const GWL_WNDPROC = (-4)
Public Const WM_MDINEXT = &H224

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    (ByVal hwnd As Long, ByVal nIndex As Long, _
    ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
    (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, _
    ByVal Msg As Long, ByVal wParam As Long, _
    ByVal lParam As Long) As Long
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
    (ByVal hWnd1 As Long, ByVal hWnd2 As Long, _
    ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Public glPrevWndProc As Long

Public Function pMyWindowProc(ByVal hw As Long, ByVal uMsg As Long, _
    ByVal wParam As Long, ByVal lParam As Long) As Long
' This is the callback procedure used when a message is received
' by this form.  The desired message is processed and all others are
' passed back to the original procedure associated with the form.
If uMsg = WM_MDINEXT Then
    'Trap and discard this message
    Exit Function
End If

pMyWindowProc = CallWindowProc(glPrevWndProc, hw, uMsg, wParam, lParam)
End Function

Public Sub pUnSubClass(lHwnd As Long)
' Stops the form from intercepting Window's messages
' by resetting the default procedures associated
' with the form.
Call SetWindowLong(lHwnd, GWL_WNDPROC, glPrevWndProc)
End Sub
Public Function fSubClass(lHwnd As Long) As Long
' Subclass the form by replacing the "normal" procedures associated
' with the form with procedure "pMyWindowProc".
' NOTE: Never add a breakpoint or pause your program in IDE
' while it is subclassed!
fSubClass = SetWindowLong(lHwnd, GWL_WNDPROC, AddressOf pMyWindowProc)
End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.