C#文本框禁用快捷键 [英] C# textbox disable shortcuts

查看:499
本文介绍了C#文本框禁用快捷键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个文本框我的Windows窗体应用程序里面,我需要禁用快捷键<大骨节病> CTRL + <大骨节病> I 和<大骨节病> CTRL + <大骨节病> ^ h 。我尝试了许多不同的解决方案,我通过谷歌找到,但它不会起作用。



我用<大骨节病> CTRL + <大骨节病> I 作为已自定义快捷方式在我的应用程序,我不希望有我的文本框里面这个命令插入一个制表位。无论出于何种原因<大骨节病> CTRL + <大骨节病> ^ h 就像按下删除?



如果我设置为false启用快捷键该控件的属性<大骨节病> CTRL + <大骨节病> I 和<大骨节病> CTRL + <大骨节病> ^ h 仍在工作。 <大骨节病> CTRL + <大骨节病> C 或<大骨节病> CTRL + <大骨节病> V 禁用即可。我希望所有的快捷键均已关闭,如果我设置为false启用快捷键。



我尝试下面的代码,我发现的地方,但它也并不妨碍<大骨节病> CTRL + <大骨节病> I 或<大骨节病> CTRL + <大骨节病> ^ h

 私人无效textBoxComment_KeyDown(对象发件人,发送KeyEventArgs E)
{
如果(e.Modifiers == Keys.Control)
{
开关(如邀请码)
{
情况下Keys.C:
情况下Keys.X:
情况下Keys.V:
情况下Keys.Z:
钥匙的情况下。我:
情况下Keys.H:
e.Handled = TRUE;
中断;
默认:
中断;
}
}
}


解决方案

尝试重写 ProcessCmdKey 功能:

 保护覆盖布尔ProcessCmdKey (参考消息味精,钥匙KEYDATA)
{
如果((KEYDATA&安培; Keys.Control)大于0和放大器;及(KEYDATA&安培; Keys.KeyCode)== Keys.V)
{
返回真;
}
返回base.ProcessCmdKey(REF味精,KEYDATA);
}


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

这篇关于C#文本框禁用快捷键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆