1

i use a textbox inside my windows form application and i need to disable the shortcuts CTRL+I and CTRL+H. I tried many different solutions i found via google but it won't work.

I use CTRL+I already as a custom shortcut in my app and i do not want to have a tabstop inserted by this command inside my textbox. For whatever reason CTRL+H acts like pressing delete?

If i set "Shortcuts enabled" to false in the properties of the control CTRL+I and CTRL+H are still working. CTRL+C or CTRL+V is disabled then. I would expect that all shortcuts are turned off if i set "Shortcuts enabled" to false.

I tried the following code i found somewhere but it also does not prevent CTRL+I or CTRL+H

    private void textBoxComment_KeyDown(object sender, KeyEventArgs e)
    {
        if ( e.Modifiers == Keys.Control )
        {
            switch(e.KeyCode)
            {
                case Keys.C:
                case Keys.X:
                case Keys.V:
                case Keys.Z:
                case Keys.I:
                case Keys.H:
                e.Handled = true;
                break;
                default:
                break;
            }
        }
    }
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
Jones
  • 11
  • 1
  • 3

4 Answers4

1

Try overriding ProcessCmdKey function:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
   if ((keyData & Keys.Control) > 0 && (keyData & Keys.KeyCode) == Keys.V)
   {
       return true;
   }
   return base.ProcessCmdKey(ref msg, keyData);
}
VladL
  • 12,769
  • 10
  • 63
  • 83
  • I already have an override for CTRL+I which triggers a function to grab to current position from axAXVLCplugin. But when i press CTRL+I i get a tab in my textbox and the current position. So both is executed. – Jones Sep 22 '13 at 10:28
1

This could help. I have ToolStripMenu with few items, which have ShorcutKeys. I wanted to stop their functionality while any TextBox has focus. This worked for me

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (ActiveControl is TextBox)
        {
            foreach (ToolStripMenuItem item in menu.DropDownItems)
            {
                if (item.ShortcutKeys == Keys.None) continue;

                if (item.ShortcutKeys == keyData)
                {
                    item.ShortcutKeys = Keys.None;

                    var ret = base.ProcessCmdKey(ref msg, keyData);

                    item.ShortcutKeys = keyData;

                    return ret;
                }
            }
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }
mathys
  • 51
  • 5
0

Use the shortcutsenabled property so you can be more selective in excluding shortcuts from mouse and from keyboard.

TextBox.ShortcutsEnabled = false;

Reference msdn Use the ShortcutsEnabled property to enable or disable the following shortcut key combinations and the control’s shortcut menu: CTRL+Z CTRL+E CTRL+C CTRL+Y CTRL+X CTRL+BACKSPACE CTRL+V CTRL+DELETE CTRL+A SHIFT+DELETE CTRL+L SHIFT+INSERT CTRL+R

FeliceM
  • 4,163
  • 9
  • 48
  • 75
  • CTRL+H and CTRL+I are still working private void MainWindow_Load(object sender, EventArgs{ textBoxComment.ShortcutsEnabled = false; } – Jones Sep 22 '13 at 10:35
  • MSDN does not explain how to 'use the ShortcutsEnabled property to enable or disable' CERTAIN key combinations, it only refers to using the boolean property to GLOBALLY turn all shortcuts on or off. The only way I can see how to eliminate unwanted shortcut combinations is to specifically eat them in a Keydown handler (unless I am missing something). – buzzard51 Oct 01 '19 at 12:43
0

Try adding e.SuppressKeyPress = true also:

private void textBoxComment_KeyDown(object sender, KeyEventArgs e)
{
    if ( e.Modifiers == Keys.Control )
    {
        switch(e.KeyCode)
        {
            case Keys.C:
            case Keys.X:
            case Keys.V:
            case Keys.Z:
            case Keys.I:
            case Keys.H:
            e.Handled = true;
            e.SuppressKeyPress = true;
            break;
            default:
            break;
        }
    }
}
King King
  • 61,710
  • 16
  • 105
  • 130
  • @Jones I tested it, you said `Ctrl + H` seems to work like a `Backspace` and it indeed acts like that when I tested, (a little supprised), however when using the code I posted, **it's really disabled**. I think you may want to post more code. – King King Sep 22 '13 at 11:11
  • I just tried with a blank form. So i added a textbox and below the function public Form1() { InitializeComponent() } i have pasted your Code. Then CTRL+I is disabled but CTRL+H is still working. I cannot add more code because i am new to StackOverflow and i have only the right to comment in the moment. But that does not explain why CTRL+I is still possible inside my other App. Perhaps because i use an Override for CTRL+I to trigger another function and because of that CTRL+I is still available inside the Textbox? – Jones Sep 22 '13 at 11:18
  • @Jones do you know how to register an event handler in `C#`? I doubt you haven't registered the handler for the `KeyDown` event. As I said, it works for me, you can try a new project and see it in action. The last question, **how do you know Ctrl + H is still enable?** Is the `character` still removed backward (like as you use `Backspace` when pressing `Ctrl + H`? – King King Sep 22 '13 at 16:08