Reputation: 61
I have a WinForm application(calculator) that is importing a composite user control. The user control has a keyDown event handler to link the keyboard to buttons on the control. When launching my program, no keyDown events are recognized or performed.
After reading through some similar posts, I now think it has something to do with the focus not being on my control. I tried doing a CalcCompCtrl1.focus() on my WinForm load to no avail. How could I go about fixing this?
Why does creating a keyDown event handler in my Form.vb and simply passing the info along not work?
Private Sub Calc_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
CalcCompCtrl1.MyCalc_KeyDown(sender, e)
End Sub
Upvotes: 0
Views: 926
Reputation: 941465
This cannot work, a UserControl is adamant at not taking the focus. It will force it to one of the client controls if you try to use the sledge-hammer. The KeyPreview hack cannot work either, it is a VB6 legacy property that UserControl does not have.
You must override ProcessCmdKey() instead. Winforms calls it before it fires the KeyDown event on the control that has the focus. Return true from the method if you used the keystroke, call base.ProcessCmdKey() if you didn't. Be selective, you don't want to swallow everything or basic stuff like keyboard navigation (cursor keys and tabbing) will stop working.
Upvotes: 1
Reputation:
Enable KeyPreview on the Form. It is a property.
Try to change the MyBase to the Variable-Name of the Calc-Object-Variable!
Upvotes: 1