箭头键和更改控件的焦点将应用程序挂起 [英] Arrow keys and changing control's focus hang the application
问题描述
我有一个用户控件,其中包含一个FlowLayoutPanel
(自上而下的流程)和一堆单选按钮.该控件公开一个CheckedChanged
事件,该事件在单选按钮的一项检查更改时触发.
I have a usercontrol that contains a FlowLayoutPanel
(topdown flow) with a bunch of radiobuttons. The control exposes a CheckedChanged
event that fires whenever one of the radiobuttons's check changed.
我的表单包含用户控件和文本框.我订阅了usercontrol的CheckedChanged
事件,并根据要选中的单选按钮,禁用文本框或将焦点放在文本框内.
My form contains the usercontrol and a textbox. I subscribe the usercontrol's CheckedChanged
event and depending on which radiobutton gets checked, I either disable the textbox or put a focus inside the textbox.
在更改单选按钮的检查状态时,所有这些都可以通过鼠标单击正常运行.但是,使用箭头键时,它将无限期地挂起.我不明白为什么会有所不同.
All this works fine with mouseclick when changing the radiobutton's check state. However, this will hang indefinitely when using the arrow keys. I don't understand why the difference.
以下是重现我所看到的行为的步骤:
The following are steps to reproduce the behavior I'm seeing:
-
创建一个用户控件,并放置一个
FlowLayoutPanel
控件并设置其FlowDirection = TopDown
.然后将两个单选按钮添加到FlowLayoutPanel
.
Create a usercontrol and drop a
FlowLayoutPanel
control and set itsFlowDirection = TopDown
. Then add two radiobuttons to theFlowLayoutPanel
.
在用户控件中提供事件处理程序
Provide an event handler in the usercontrol
public event EventHandler CheckedChanged
{
add { radioButton2.CheckedChanged += value; }
remove { radioButton2.CheckedChanged -= value; }
}
创建一个Windows窗体并删除上面的用户控件.添加一个文本框并将Enabled
设置为False.订阅用户控件的CheckedChanged
事件,如下所示
Create a windows form and drop the above user control. Add a textbox and set Enabled
to False. Subscribe to the usercontrol's CheckedChanged
event as follows
private void userControl11_CheckedChanged(object sender, EventArgs e)
{
textBox1.Select();
}
运行.请注意,如果使用鼠标在单选按钮之间单击,则一切正常.但是如果使用向上/向下箭头键,它将崩溃.
Run. Notice that if you use the mouse to click between the radiobuttons, thing works fine; but it will crash if you use the up/down arrow keys.
推荐答案
public event EventHandler CheckedChanged
{
add {
radioButton2.CheckedChanged += value;
}
remove {
radioButton2.CheckedChanged -= value;
}
}
嗯,value
未初始化?还是我错过了什么?
Hmm, value
is uninitialized? Or am I missing something?
这篇关于箭头键和更改控件的焦点将应用程序挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!