需要诸如用户控件中的“接受"按钮之类的东西 [英] Need something like Accept Button in User Control
问题描述
我需要在Windows应用程序中的UserControl
(不是Form
)上有一个Button
,以响应"Enter"命中,即将按钮设置为
I need to have a Button
on a UserControl
(not Form
) in a windows application to respond to "Enter" hit, the way a button which is set as the Accept Button
of a Form
works.
我无法使按钮成为焦点,因为我需要通过选项卡更改来使其他控件成为焦点.
I cannot make the button to be focused, since I need other controls to be focused with tab change.
我们非常感谢您的帮助:)
Any help is really appreciated :)
推荐答案
AcceptButton是Form的属性,不能用于UserControl.但是,您可以简单地覆盖ProcessCmdKey,只要用户控件具有焦点,它就可以工作.否则,如果您有多个可能处于活动状态的控件,则需要单独使用表单的AcceptButton或覆盖表单中的ProcessCmdKey.
The AcceptButton is a property of Form and cannot be used for the UserControl. You can simply override ProcessCmdKey though and this will work so long as the user control has focus. Otherwise you will need to use the AcceptButton of the form separately or override the ProcessCmdKey in the form if you have multiple controls which could be active.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
button.PerformClick();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
这篇关于需要诸如用户控件中的“接受"按钮之类的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!