Reputation: 5
I am making a game, and to open up and close the store, you press S. While in the store, you have six different choices to buy from, but they are all buttons.
However, once you buy something, the focus is no longer on the form, but on the button, and the key down event is part of the form, therefore, because the focus gets switched from the form to the button, the key down event no longer works, and disables you from closing the store and continuing on with the game.
My question is how to set the focus back to a form once a button is press? I started out with visual basic, and the code would be something along the lines of form1.setfocus
, but its totally different in c#.
I have tried Activating the form, .focus
, a lot, and nothing seems to be setting the focus back to the form. Help would be greatly appreciated.
Upvotes: 0
Views: 2050
Reputation: 3760
Form1.focus();
But I think, to get keyboard events on Form
itself, you need KeyPreview
set to true
for the Form
so that Form gets Keys first and then other controls.
Upvotes: 1
Reputation:
Try:
form.Focus();
MSDN:
The Focus method returns true if the control successfully received input focus. The control can have the input focus while not displaying any visual cues of having the focus. This behavior is primarily observed by the nonselectable controls listed below, or any controls derived from them.
Upvotes: 0