0

I have a form with keypreview true. OnKeyPress (if key is escape) I was closing the form.

Later I set autocomplete with the first text box in form. Autocomplete is working but OnKeyPress event is not working now. If cursor is not in text box having auto-complete OnKeyPress works and still close the form on escape key.

Please guide how I can have both ? Autocomplete with closing form on escape.

laurent
  • 83,816
  • 72
  • 267
  • 404
haansi
  • 5,192
  • 19
  • 58
  • 91

1 Answers1

2

In your application you should not use form events for closing (because any control on your form can handle OnKeyPress by it's own and swallow it), you should register the hotkey.

[DllImport("user32.dll")]
private static extern bool RegisterHotKey (int hwnd, int id, int fsModifiers, int vk);

[DllImport("user32.dll")]
private static extern bool UnregisterHotKey (int hwnd, int id);

Refer to here or to here

Community
  • 1
  • 1
Sasha Reminnyi
  • 3,372
  • 22
  • 26