1

I'm trying to extend TabControl to be able to hide all tabs. Based on Hans Passant answer (https://stackoverflow.com/a/2207774/965722) I've created code like below:

using System;
using System.Windows.Forms;

class ViewStack : TabControl
{
    protected override void WndProc(ref Message m)
    {
        // Hide tabs by trapping the TCM_ADJUSTRECT message
        if (m.Msg == 0x1328 && !DesignMode) m.Result = (IntPtr)1;
        else base.WndProc(ref m);
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Control | Keys.Tab) || keyData == (Keys.Control | Keys.Shift | Keys.Tab) || keyData == (Keys.Left) || keyData == (Keys.Right))
        {
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

This way tabs are visible in design mode and hidden in executable. What I need to do is to disable all possible built-in keyboard shortcuts for this control so that changing tab will be available only by code.

For now I have blocked navigation with Ctrl + Tab, Ctrl + Shift + Tab and using Left/Right arrows.

What other shortcuts should I block so that end user won't be able to change tabs on his own (using any key combination that is build in TabControl)?

Community
  • 1
  • 1
Misiu
  • 4,738
  • 21
  • 94
  • 198
  • 1
    Home, End, Ctrl+PageUp, Ctrl+PageDown. – Hans Passant Aug 28 '12 at 14:03
  • 1
    Why not simply remove them from the control and keep them in an array so you can re-add them when you need them ? That way you won't have to worry about the key combinations. – denied66 Aug 28 '12 at 14:33
  • @denied66 - so Your saying it would be better to normally create TabControl and then at runtime remove all tabs and add them on demand? I would like to stay with my idea, because it will be easier to edit content on 'invisible' tabs without need to add them. Could You show Your solution as answer? I'm looking for simple way to switch content on a form and this seems the easiest :) – Misiu Aug 28 '12 at 16:47
  • @HansPassant - Thanks Hans, do I need to override ProcessCmdKey or something else too? – Misiu Aug 28 '12 at 17:21
  • 1
    TabControl has unusual keyboard handling. Why don't you just filter *everything* with ProcessCmdKey? Just return true. – Hans Passant Aug 28 '12 at 17:26
  • @HansPassant - That's an option :) I'll try it. Won't I remove shortcut handling in child control? For example if I as normal TabControl to my extended class? Anyway thanks Hans, for suggestions and base code :) – Misiu Aug 28 '12 at 18:24
  • @HansPassant - If I use `return true` then all keys are blocked, for example if I add textBox to tab then I can't enter any text to it. Can this be fixed? – Misiu Sep 13 '12 at 10:27

1 Answers1

0

You can use:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {            
        if(keyData == (Keys.Control | Keys.Tab) || keyData == (Keys.Control | Keys.Shift | Keys.Tab) || keyData == Keys.Left || keyData == Keys.Right || keyData == Keys.Home || keyData == Keys.End) {
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

This should resolve your problem.