2

I am wondering if it is possible to handles KeyPress event at form level when there are controls in a form.

I can achieve this when there is no control on the form, but when i add something, like a button, the form loses the focus and i can't give it back, even with Me.Focus. The focus stays on the button.

Is there a way to do it ? If not, i would like to know why. Looks interesting.

Noob dev
  • 121
  • 10
  • 3
    Enable the [KeyPreview](https://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview(v=vs.110).aspx) property of the container (Form) – Jimi Dec 28 '17 at 17:21
  • Try a KeyDown event for your form instead – jdwee Dec 28 '17 at 17:22
  • Try to disable all the event before you press required KEY and after press you enable all those that you disabled. storage those (events) in an typed variable. – Ajay Kumar Oad Dec 28 '17 at 17:23
  • 1
    Search for overriding ProcessCmdKey. – LarsTech Dec 28 '17 at 17:31
  • The character produced by a key press goes to the control with the focus. So never the form when it has controls, hopefully it will be a control like TextBox. This rarely stumps the user, they just don't type anything unless it looks like it produces text. Nor does the Form class have any idea what to do with it, it cannot display the character. Using the KeyDown event is still somewhat sensible, it can be used to implement shortcut keystrokes. Like F1 to display help. – Hans Passant Dec 28 '17 at 17:34
  • Thanks for your answers ! The most simple way to make it work was @Jimi answer : Just enabled the KeyPreview property and everything works now :) – Noob dev Dec 29 '17 at 08:59

2 Answers2

1

Just set true to key preview property of your form it will work.

Abhay shah
  • 76
  • 5
0

You can do it by enabling KeyPrivew Property of your form.enter image description here

Let's Enkindle
  • 57
  • 2
  • 17