Avoiding alert on key board shortcut of C# winform

给你一囗甜甜゛ 提交于 2019-12-10 22:28:06

问题


I am making key board shortcuts to a Winform application in C# using Visual Studio 2012. My shortcuts work perfect. But it gives a annoying beep sound.

I added e.Handled = true; and e.SuppressKeyPress = true; according to many threads. But it does not work and my winform stuck.

How can I avoid this?

private void textBoxSearch_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Down)
        {
            do stuff
        }
        else if (e.KeyCode == Keys.Enter)
        {
            //do stuff
        }
        e.Handled = true;
        e.SuppressKeyPress = true;
    }

and I need a solution for this too.

 protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Control | Keys.F))
        {
            //do stuff
        }
        else if (keyData == (Keys.Control | Keys.G)) {
            //do stuff
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }

回答1:


What you have in the KeyDown event should work. The SupressKeyPress = true stopped the ding for me when I copied your code.

In the ProcessCmdKey event you would need this:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.Control | Keys.F))
    {
        //do stuff
        return;
    }
    else if (keyData == (Keys.Control | Keys.G)) {
        //do stuff
        return;
    }

    return base.ProcessCmdKey(ref msg, keyData);
}


来源:https://stackoverflow.com/questions/24487086/avoiding-alert-on-key-board-shortcut-of-c-sharp-winform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!