开发者

Windows form has focus but doesn't process keyboard events

开发者 https://www.devze.com 2023-03-31 18:42 出处:网络
I have a strange problem: I have a Form that I open using ShowDialog(). The开发者_运维知识库 form is populated with some buttons and comboboxes. One of the comboboxes is set as the ActiveControl of t

I have a strange problem:

I have a Form that I open using ShowDialog(). The开发者_运维知识库 form is populated with some buttons and comboboxes. One of the comboboxes is set as the ActiveControl of the Form and the Form has focus.

What I want to accomplish is that the user can enter its username immediately after the Form opens (without the need to select the combobox first). However, if I press the keyboard, nothing happens. However, when I first click on the form with the mouse, and then enter something using the keyboard, it works. I already tried a lot of things like calling Select() and Focus() on the form. I even tried to simulate a mouseclick event (OnMouseClick) on the Form without any luck.

Someone has an idea would could be the problem here?

many thanks

Chris


Try BringToFront()

    var f = new Form1();
    f.Show();
    f.BringToFront();

Then just use Select on that control

    private void Form1_Load(object sender, EventArgs e)
    {
        comboBox1.Select();
    }


Assuming you're running ShowDialog() from another form it might be worth changing it to: ShowDialog(this) so the new form has the correct parent and the correct blocking behaviour. Without the 'this' I've seen forms open up behind other forms and other strange behaviour, including focus problems.

Just a thought.


Have you tried calling myComboBox.Focus();

Just because the form has focus doesn't necessarily mean any control within the form has focus. Also try checking the onKey events of both the form and the individual controls. This normally helps me diagnose what exactly has focus. If the form itself and non on its controls are getting any onKey events then try using form.Activate();

0

精彩评论

暂无评论...
验证码 换一张
取 消