为c#中的按钮点击分配快捷方式 [英] Assigning shortcuts to button clicks in c#

查看:99
本文介绍了为c#中的按钮点击分配快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个项目,我的教练希望为退出按钮分配快捷方式alt-x。到目前为止,我有

Im working on a project and my instructor wants the exit button to be assigned the shortcut alt-x. So far i have

         private void Form1_Load(object sender, EventArgs e)
         {
             radAddition.Checked = true;
             radSmallRange.Checked = true;
             picCorrect.Visible = false;
             picWrong.Visible = false;
             this.KeyPreview = true;

         }
private void Form1_KeyDown(object sender, KeyEventArgs e)
         {
            if (e.Modifiers == Keys.Alt && e.KeyCode == Keys.X)
            {
                this.btnExit.PerformClick();
            }
         }
private void btnExit_Click(object sender, EventArgs e)
         {
             DialogResult result;
             result = MessageBox.Show("Are you sure you want to exit?",
                 "Exiting",
                 MessageBoxButtons.YesNo);
             if (result == DialogResult.Yes)
             {
                 this.Close();
             }
         }



我只是复制并粘贴了这些东西,所以并非一切都是相关的,但所有相关的东西都在那里。但是,当我按alt-x注意发生!我究竟做错了什么?提前谢谢!


I just copied and pasted the stuff so not everything is relevant but everything relevant is there. But when i press alt-x noting happens! what am i doing wrong? thanks in advance!

推荐答案

您为KeyDown事件创建了事件处理程序,但我们无法看到您是否在事件的委托列表中设置了此处理程序。

此外,由于你想使用带ALT键的快捷键,可能有一种更简单的方法来完成你想做的事。



使用事件处理程序:



只需在表单的构造函数中插入此语句:

You created the event-handler for the KeyDown event, but we cannot see if you set this handler in the event's delegate list.
Moreover, as you want to use a shortcut with ALT key, there may be a simpler way to accomplish what you want to do.

Using your event handler:

Just insert this statement in your form's constructor:
this.KeyDown += Form1_KeyDown;





您还可以创建一个专门的方法 ConfirmExit ,你可以从你的事件处理程序调用(一般来说,我真的不喜欢事件处理程序中的直接实现)。



You may also create a specialized method ConfirmExit, which you would call from both your event-handlers (in general, I really don't like direct implementations in event handlers).

private void ConfirmExit()
{
   DialogResult result = MessageBox.Show("Are you sure you want to exit?", "Exiting", MessageBoxButtons.YesNo);
   if (result == DialogResult.Yes)
   {
      this.Close();
   }
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.Alt && e.KeyCode == Keys.X)
   {
      ConfirmExit();
   }
}

private void btnExit_Click(object sender, EventArgs e)
{
   ConfirmExit();
}



一个实现,用于两个不同的地方。干净,易于阅读/遵循。



使用更简单的解决方案:

如您所愿ALT键,你可以使用内置的功能:在你的按钮的Text属性中,在你用作快捷键(X键)的字母前加上&符号(& )。

示例:


One implementation, used in two different places. Clean and easy to read/follow.

Using a much simpler solution:
As you want to use the ALT key, you may use an in-built functionnality: in your button's Text property, prefix the letter you want to use as shortcut (the X key) with the ampersand sign (&).
Example:

btnExit.Text = "E&xit";



您可以在设计模式下从属性网格中执行此操作。

字母X将加下划线,这意味着您可以通过按ALT + X来激活键盘上的按钮。

当然,如果出现以下情况,这不是解决方案:

- 你想要另一个组合键而不是ALT;

- 或者你想要的快捷方式字母没有出现在按钮的Text属性中。



希望这会有所帮助。



编辑:KeyDown处理程序中的简化alt测试。


You can do that in design mode from the property grid.
The letter X will be underlined, meaning that you can activate the button from keyboard by pressing ALT + X.
Of course, this is not a solution if:
- you want another combination key than ALT;
- or the letter you want as shortcut does not appear in the button's Text property.

Hope this helps.

Edited: simplified alt test in KeyDown handler.


这篇关于为c#中的按钮点击分配快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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