Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when an application loses focus?

Tags:

c#

winforms

I created a usercontrol in c#, Leave and LostFocus are not fired if I switch to another program. How can I detect if the app loses focus?

like image 544
Hao Avatar asked Oct 16 '09 05:10

Hao


2 Answers

Try your mainform's Deactivate event

like image 190
junmats Avatar answered Sep 22 '22 07:09

junmats


If you switch to a differnt app, your custom control DIDN'T lose focus. At least not within the app it is in.

If you come back to your app, such as hitting it on the taskbar (and not by clicking on another control on the same app), your control will still have "focus".

This is where Form.Deactivate comes in handy.

like image 29
Neil N Avatar answered Sep 21 '22 07:09

Neil N