Solved

Event when form loses focus?

Posted on 2004-09-01
9
532 Views
Last Modified: 2013-12-25
Hi,

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!
0
Comment
Question by:Ander123
  • 2
  • 2
  • 2
  • +1
9 Comments
 
LVL 6

Expert Comment

by:msjammu
ID: 11950161
I think You do it in Dactivate Event Instread

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

Regards,
0
 
LVL 6

Expert Comment

by:msjammu
ID: 11950168
Dactivate  event is triggered in both the cases :
In case the form is minimized and when User shift to other window
0
 
LVL 15

Expert Comment

by:ameba
ID: 11952367
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)
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 17

Expert Comment

by:zzzzzooc
ID: 11957016
To add to ameba's proposed solution, here's an example I submitted not too long ago..

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21082964.html
0
 

Accepted Solution

by:
Ander123 earned 0 total points
ID: 12000968
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

      CopyToClipboard

    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.  :?)
0
 
LVL 15

Expert Comment

by:ameba
ID: 12001727
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"
0
 

Author Comment

by:Ander123
ID: 12061315
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.)
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question