箭头键和更改控件的焦点会使应用程序挂起

时间:2022-08-16 17:25:54

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包含一个带有一组radiobuttons的FlowLayoutPanel(自上而下流)。该控件公开CheckedChanged事件,只要其中一个radiobuttons的检查发生更改,该事件就会触发。

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.

我的表单包含usercontrol和一个文本框。我订阅了usercontrol的CheckedChanged事件,并根据检查的radiobutton,我要么禁用文本框,要么将焦点放在文本框中。

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.

当改变radiobutton的检查状态时,所有这一切都可以正常使用鼠标点击。但是,使用箭头键时,这将无限期挂起。我不明白为什么不同。

The following are steps to reproduce the behavior I'm seeing:

以下是重现我所看到的行为的步骤:

  1. Create a usercontrol and drop a FlowLayoutPanel control and set its FlowDirection = TopDown. Then add two radiobuttons to the FlowLayoutPanel.

    创建一个usercontrol并删除FlowLayoutPanel控件并设置其FlowDirection = TopDown。然后将两个radiobuttons添加到FlowLayoutPanel。

  2. Provide an event handler in the usercontrol

    在usercontrol中提供事件处理程序

    public event EventHandler CheckedChanged
    {
        add { radioButton2.CheckedChanged += value; }
        remove { radioButton2.CheckedChanged -= value; }
    }
    
  3. 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

    创建一个Windows窗体并删除上面的用户控件。添加文本框并将Enabled设置为False。订阅usercontrol的CheckedChanged事件,如下所示

    private void userControl11_CheckedChanged(object sender, EventArgs e)
    {
        textBox1.Select();
    }
    
  4. 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.

    跑。请注意,如果您使用鼠标在单选按钮之间进行单击,则可以正常工作;但如果使用向上/向下箭头键,它将崩溃。

1 个解决方案

#1


public event EventHandler CheckedChanged
{
    add {
         radioButton2.CheckedChanged += value;
        }
    remove {
         radioButton2.CheckedChanged -= value;
        }
}

Hmm, value is uninitialized? Or am I missing something?

嗯,价值未初始化?或者我错过了什么?

#1


public event EventHandler CheckedChanged
{
    add {
         radioButton2.CheckedChanged += value;
        }
    remove {
         radioButton2.CheckedChanged -= value;
        }
}

Hmm, value is uninitialized? Or am I missing something?

嗯,价值未初始化?或者我错过了什么?