Event when form loses focus?


I'd like my app to copy some specific data to the clipboard as soon as its form loses focus (when it's minimized, or when the user switches to another window).

I thought I could simply use the .LostFocus event for the form. I'm simply doing this:

  Private Sub Form_LostFocus()

    [code to copy data to clipboard]

  End Sub

...but it doesn't seem to have any effect. Maybe I'm using it wrong?

I know could keep a timer running to periodically check if the app still had the focus, but that seems kludgy. Isn't there a smoother way? Thanks!
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Ander123Connect With a Mentor Author Commented:
Mstammu: Thanks for your suggestion. However, I couldn't get Form_Deactivate to work. I tried a test form with:

  Private Sub Form_Deactivate()

    Me.Caption = "I lost the focus!"

  End Sub

...but it had no effect when I switched to another window. VB's Language Reference says:

  "The Activate and Deactivate events occur only when moving the focus within an application. Moving the focus to or from an object in another application doesn't trigger either event."

Odd, isn't it, that Microsoft didn't provide an easy way to do such an obviously useful thing?

ameba & zzzzzooc: Yes, subclassing is amazing---but for this simple function, I think I'll just use a timer and an API call after all. When the user selects a data item in a list, I start a timer that checks the GetActiveWindow API at half-second intervals. When the app loses focus, it triggers the clipboard-copy:

[in declarations:]

  Private Declare Function GetActiveWindow lib "user32" () as long

[in main code:]

  Private Sub tmrCheckFocus_Timer()

    If GetActiveWindow = Me.hwnd Then


    End If

  End Sub

It works fine---and I figure that one check every half-second won't put much of a drain on anyone's resources.  :?)
I think You do it in Dactivate Event Instread

Private Sub Form_Deactivate()
    [code to copy data to clipboard]
End Sub

Dactivate  event is triggered in both the cases :
In case the form is minimized and when User shift to other window
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Hi Ander123,

To catch when your application is deactivated, you must use subclassing:

"To get a notification when the application as a whole gets or loses the input focus, you must subclass
a form in your application, and watch for the WM_ACTIVATEAPP message. When this message is received,
the wParam argument holds zero if the application is losing the focus, or a non-zero value if the application
is getting the input focus." (from old vb2themax site)
To add to ameba's proposed solution, here's an example I submitted not too long ago..

Yes, that might be good choice (once you know your choices).  No-one mentioned it because you said  "I know I could keep a timer running to periodically check if the app still had the focus"
Ander123Author Commented:
Hey, I know you guys are good! (Since I'd decided to use that method after all, I wanted to post the code in case it was useful to anyone else.)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.