C# – ALT+ precedes KeyPress events on custom textbox winform control

ccustom-controlswinforms

I'm having a rather tricky problem that I'll try to explain as good as possible. The control is a textbox built from scratch. It inherits from System.Windows.Forms.Control.

Now, on the form, I have a few other controls with the & shortcut (ALT+).

When a non-input control is focused in winforms, you can exclude ALT in these shortcuts and just press the . This is the exact same behavior that I'm experiencing with my textbox.

This results is that before the KeyPress event is fired, the focus shifts to another control and KeyPress never gets fired. The KeyDown event is fired though.

I get the feeling my control is not recognized as an input control.

Another point of interest is that if I set SuppressKeyPress = true during the KeyDown event, the focus stays on my control, but of course the KeyPress event is not fired either.

Best Answer

You should override ProceeMnemonicMethod in your control. Also you can view for ProcessDialogKey and ProcessCmdKey methods for better understanding of internal WinForms keyboard processing.

Related Topic