Okay,

How do I make MainFormKeyDown absolute GOD over everything else? I have a tabpage control, and I want CTRL+Tab to ALWAYS switch tabs. But several tabs have a Multiline edit controls that "WantTabs". I added into the EditKeyDown a handler for "Shift+Tab" to make it go back from a previous tab. But no matter what I do in the MainFormPreviewKeyDown, MainFormKeyDown, or InputEditKeyDown, I never receive any signals for Ctrl+Tab. WTF? Is KeyPreview = True a Lie, because it sure seems that way on my end.

Thanks
Jaeden "Sifo Dyas" al'Raec Ruiner

Dani AI

Generated

KeyPreview is not a lie — it just does not cover every place Windows/WinForms can intercept keys. is seeing Ctrl+Tab disappear because Tab (and combos like Ctrl+Tab) is treated as a navigation/command key in the message-preprocessing pipeline, so it can be handled before the form's KeyDown/KeyPreview events run. 's global hook will catch it, but that is overkill for an in-process app.

The usual, simple fix is to handle it at the command-key level by overriding the form's ProcessCmdKey. Put this in the main form and switch the TabControl yourself:

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
    If (keyData And Keys.KeyCode) = Keys.Tab AndAlso (keyData And Keys.Control) = Keys.Control Then
        Dim direction As Integer = If((keyData And Keys.Shift) = Keys.Shift, -1, 1)
        Dim tc = Me.TabControl1 ' point this at your TabControl
        Dim idx = tc.SelectedIndex + direction
        If idx < 0 Then idx = tc.TabCount - 1
        If idx >= tc.TabCount Then idx = 0
        tc.SelectedIndex = idx
        Return True ' handled
    End If
    Return MyBase.ProcessCmdKey(msg, keyData)
End Function

Alternatives: implement an IMessageFilter (Application.AddMessageFilter) to watch WM_KEYDOWN, or override WndProc, but both are more low-level than ProcessCmdKey and require careful cleanup. Also check control properties like AcceptsTab on multiline editors if you want plain Tab to insert tabs. In short: KeyPreview works for normal KeyDown events, but to catch Ctrl+Tab reliably in WinForms override ProcessCmdKey (or use a message filter) rather than relying only on KeyPreview.

Recommended Answers

All 3 Replies

Is KeyPreview = True a Lie, because it sure seems that way on my end.

It's not a lie, but you might be confused about when and how a control captures key strokes. I think you want a . another link.

It's not a lie, but you might be confused about when and how a control captures key strokes. I think you want a global winndows hook

Well, that appears to be a lie to me then.

MainForm.KeyPreview is a binary equation.
1: Either it Previews EVERY key
or
2: It doesn't.

If it doesn't then it's a lie, and doesn't do what it says it should do.

In Delphi when you have keypreview for the main form it receives EVERY key code before any other control in the application. That's what "KeyPreview" means. But I guess I shouldn't be surprised, since KeyPreview has never worked for VBA ever, so why should they fix it now. *shrug*

A global windows hook is a bit overkill, because i'm not trying to link a key command into the system, just into my application.

*shrug*

Jaeden "Sifo Dyas" al'Raec Ruiner

Well, that appears to be a lie to me then.

If that's how you feel, who am I to disagree?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.