0

I've got a winform application. In the application I have a Panel with multiple Buttons.

Now when the Buttons don't have the Focus I can capture the keypressed Events in the form itself. But when the Buttons have the Focus the form (even if the Buttons don't catch the Event explecitely) only they get the keypressed Event and not the form.

Now my question is: Is there any way to centralize the keypressed behaviour (without creating a keypressed Event for each and every button and call a central method with that Event)?

Thomas
  • 2,886
  • 3
  • 34
  • 78
  • Split the difference: create a single handler and _attach_ it to each button's event. One code called by all. – DonBoitnott Aug 07 '17 at 13:58
  • @DonBoitnott I know that method. It would mean I would have to use the keypressed event for every button. Thus I wondered if it was possible to avoid just that – Thomas Aug 07 '17 at 16:00

3 Answers3

0

In essence only 1 method needs to be defined with the appropriate Parameters: Example:

 private void Event_Key_Press_Check(object sender, KeyPressEventArgs e)

This method then only Needs to be put in as the Name of the method used in the Event (form designer), or added as the Event.

That way only 1 method is used.

Thus there is no shorter way and the Event Needs to be defined for every single button (instead of 1 central Event that is always triggered).

Thomas
  • 2,886
  • 3
  • 34
  • 78
0

Set form property KeyPreview to true and set KeyPress handler. Then form will handle this event before buttons.

See KeyPreview MSDN documentation.

0

I've had the same issue, and it was pretty easy to resolve :)

Check here : KeyPress at form level with controls

Just set the KeyPreview property (of your form) to True. This way your form will handle KeyPress event before any other control, even if one of them has the focus

Noob dev
  • 121
  • 10