当用户按下ctrl +向左箭头时,如何执行按钮点击事件 [英] how to execute Button click event when user press on ctrl+left arrow

查看:120
本文介绍了当用户按下ctrl +向左箭头时,如何执行按钮点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i是Windows应用程序的新手。当我按(轨迹+左)箭头按钮。按钮点击事件必须触发。如果没有控件是否有形式它工作正常。但如果任何控件在形式它不工作。因为焦点正在进行控制。请帮助我这里的任何一个是我的代码..





Hi every one,

i am new to windows application.when i press (contrail+left) arrow button.button click event has to fire.if no controls are there in form it is working fine.but if any controls are there in form it is not working.because focus is going on to that control.please help me any one here is my code..


private void Short_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
       {
           if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.Left)
           {
               MessageBox.Show("left");
               // button1.Focus();

           }
           else if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.Right)
           {
               MessageBox.Show("right");
            
           }

       }

推荐答案

可能你忘了一个细节:到将表单的属性 KeyPreview 设置为true。小心使用:默认情况下,它会故意设置为false。请参阅:

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview%28v=vs.110%29.aspx [ ^ ]。



-SA
Probably you forgot one more detail: to set the property KeyPreview to true for your form. Use with care: it is set to false by default, on purpose. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview%28v=vs.110%29.aspx[^].

—SA


你的问题在这里,因为我认为你已经意识到,对于Form上的Controls,Form永远不会有'Focus,所以:PreViewKeyDown EventHandler永远不会被激活。



使用KeyDown事件:
Your problem here, as I think you've already become aware, is that with Controls on the Form, the Form is never going to have 'Focus, so: the PreViewKeyDown EventHandler is never going to fire.

Use the KeyDown Event instead:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (Control.ModifierKeys == Keys.Control)
    {
        switch (e.KeyCode)
        {
            case Keys.Left:
                Console.WriteLine("control-left");
                button1.PerformClick();
                break;
            case Keys.Right:
                Console.WriteLine("control-right");
                break;
        }
    }
}

我建议您在调试时使用Console.WriteLine而不是使用'MessageBox.Show:它不那么侵扰性。



最后,我想要注意的是,使用像这样的按键组合模拟按钮单击是一种非常不寻常的设计选择,可能会让用户感到困惑。如果有一个常用的方法会更好,imho,它将被点击的Button和键组合调用。

I suggest you use Console.WriteLine while debugging rather than using 'MessageBox.Show: it's less "intrusive."

Finally, I'd like to note that simulating a Button Click using a key-combination like this is a very unusual design choice, and one that may confuse users. It would be better, imho, to have a common method that would be called by both the Button being clicked, and by the key-combination.


这篇关于当用户按下ctrl +向左箭头时,如何执行按钮点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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