Panel not getting focus

后端 未结 6 2099
小鲜肉
小鲜肉 2020-11-22 14:42

I am continuing to program some kind of keyboard navigation in my simple graphic program (using C#). And I ran into trouble once again.

6条回答
  •  醉酒成梦
    2020-11-22 15:06

    The simplest trick I use when for any reason I can’t use the parent Form KeyPreview property to make the Form handle key events, is to put a Textbox on

    The panel:

    Panel.Controls.Add(_focusTextBox = new TextBox() { Visible = true , Left = -300, TabIndex = 0});   
    

    And use it to capture KeyDown event:

    _focusTextBox.KeyDown += panel_KeyDown;
    

    The last step is to set focus to this TextBox when other controls on the panel clicked:

    _focusTextBox.Focus();
    

提交回复
热议问题