[英]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: 例:
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.1
, 1.2
,而不是仅仅1
和2
),但如果标签和文本框是相同的容器内,应该不会太难正确设置。
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.