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
  • 88,262
  • 77
  • 290
  • 428
haansi
  • 5,470
  • 21
  • 63
  • 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,442
  • 2
  • 23
  • 27