0

I've got a winform application (Formname f1). Inside that form is a Panel (p1) with 2 Buttons (b1 and b2).

The Panel is by Default enabled = false and visible = false.

Now when I press enter in the form (keypress Event) I make the Panel visible and enabled.

When I then click on a button (b1) and press enter again (preview_keyodwn Event) I do the following:

f1.ActiveForm.Focus
p1.Enabled = false;
p1.visible = false;

Now though when I press enter the Panel does not come visible again. After checking why I found out that the Focus is STILL on the button instead of the form itself.

Is there any way to set the Focus away from the button onto the form itself when the Panel goes invisible and disabled?

Thomas
  • 2,886
  • 3
  • 34
  • 78
  • try TabStop = false or increase the TabIndex – live2 Aug 08 '17 at 08:15
  • A better solution is to override Form.ProcessCmdKey. Then it doesn't matter which Control has focus! – Jonathan Willcock Aug 08 '17 at 08:24
  • @JonathanWillcock that is a very good Point tnx didn't know about that Events existence. I will use that one instead (ist a better way than what I did.). – Thomas Aug 08 '17 at 08:47
  • In all honesty though I'm not sure if duplicate is correct. For the usecase itself the process keycmd is the better solution but with the Approach taken and asked in the question it goes into a different direction than the "duplicated" question. – Thomas Aug 08 '17 at 09:16

1 Answers1

0

Call the select method after hiding the panel

p1.Enabled = false;
p1.visible = false;
f1.ActiveForm.Focus();
f1.ActiveForm.Select();