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?

[Webinar] Streamline your web hosting managementRegister Today

Erick37Connect With a Mentor Commented:
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
turn on the KeyPreview property of the form, and write your code in key_down/key_press/key_up events.
All Courses

From novice to tech pro — start learning today.