简体   繁体   English

如何在Windows窗体应用程序中创建Alt快捷方式?

[英]How can you create Alt shortcuts in a Windows Forms application?

I'd like to create keyboard shortcuts for some controls in my Windows Forms application. 我想为我的Windows窗体应用程序中的某些控件创建键盘快捷键。

Example: 例:

XYZ的屏幕截图

Notice the underlined, FEVP B. 请注意下划线,FEVP B.

I have a label and a textbox control. 我有一个标签和一个文本框控件。 I'd like to associate that Alt keyboard shortcut to the label and the textbox. 我想将Alt键盘快捷键与标签和文本框相关联。 So if someone presses Alt + B , focus is given to the associated textbox. 因此,如果有人按下Alt + B ,则会关注相关的文本框。 Is there a way to create this association? 有没有办法创建这种关联?

When the label receives focus from pressing its accelerator key (set using the & ), it forwards the focus to the next control in the tab order, since labels are not editable. 当标签通过按下其加速键(使用&设置)获得焦点时,它会将焦点转移到Tab键顺序中的下一个控件,因为标签不可编辑。 You need the textbox to be next control in the tab order. 您需要在Tab键顺序中将文本框作为下一个控件。

To view and correct the tab order of your form, use the View + Tab Order command in the IDE. 要查看和更正表单的Tab键顺序,请使用IDE中的View + Tab Order命令。 Using TabPages or other containers adds a level of nesting to the tab order (eg, 1.1 , 1.2 instead of just 1 and 2 ), but if the label and textbox are within the same container it shouldn't be too hard to set properly. 使用的TabPages或其他容器中添加嵌套的标签顺序的水平(例如, 1.11.2 ,而不是仅仅12 ),但如果标签和文本框是相同的容器内,应该不会太难正确设置。

Type &File or &Edit and you will get underline. 输入&File&Edit ,您将获得下划线。 That will automatically bind underlined letters with Alt keyword for shortcut. 这将自动将带下划线的字母与Alt关键字绑定为快捷方式。

EDIT. 编辑。 You question has modified so I'd like to keep up with my answer. 你的问题已修改,所以我想跟上我的回答。 You would like to catch some keys combination ( Alt + F ) and set a focus to the text box. 您想要捕捉一些键组合( Alt + F )并将焦点设置到文本框。

You may try this solution using KeyDown event of the main form. 您可以使用主窗体的KeyDown事件尝试此解决方案。

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Alt && e.KeyCode == Keys.F)
        {
            this.textBox1.Focus();
        }
    }

To achieve this, you have to additionally set KeyPreview property of the form to true . 为此,您必须另外将表单的KeyPreview属性设置为true

this.KeyDown += new KeyEventHandler(Form1_KeyDown);

void Form1_KeyDown(object sender, KeyEventArgs e) 
{ 
 if (e.Alt && e.KeyCode == Keys.W)
 { 
   btnShowConstructionCdFun(); 
 } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为单个 windows forms 应用程序发布两个不同的快捷方式? - How to publish two different shortcuts for a single windows forms application? Windows窗体中的键盘快捷键 - Keyboard shortcuts in Windows Forms 如何创建第一个Windows Forms DevExpress应用程序 - How to create first Windows Forms DevExpress application 如何为Windows窗体应用程序创建安装程序和卸载程序 - How to create an installer and uninstaller for a Windows Forms application 在 Windows 窗体应用程序中实现键盘快捷键的最佳方法? - Best way to implement keyboard shortcuts in a Windows Forms application? 如何使表单最大化到Windows窗体应用程序中的任何计算机屏幕? - How can you make the form maximize to any computer screen in a Windows Forms application? 您可以从Windows窗体应用程序公开XML Web服务吗? - Can you expose XML webservices from a Windows Forms Application? 您可以在Mac版Xamarin上启动Windows窗体应用程序吗? - Can you start a Windows Forms Application on Xamarin for Mac? 如何让我的Windows窗体程序执行任何SendTo快捷方式 - How can I make my Windows Forms program execute any of the SendTo shortcuts 如何实现Windows表单应用程序C#的样式? - How do you implement a Styling for Windows forms Application C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM