简体   繁体   中英

C# textbox disable shortcuts

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;
            }
        }
    }

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);
}

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);
    }

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

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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM