How do one detect that a form lost focus when program loses focus i.e. user go to another program for instance? Program is open and form is open but I go to another program.
LostFocus event of open form doesn't fire when I click out of my program to another program.
There is functionality in my program that I need to turn off when the user go to another program for instance and turn it back on when the user comes back to the program. But it's on a form level that I need to turn the functionality on/off.
Everything work with Activate/Deactivate events when jumping between forms in the program but I can't get it to work when leaving a form open and jumping in/out of the program.
Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.
Public Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hWnd As Long) As Long
vb6forever, SetFocus doesn't detect anything though. It was my understanding he wanted to "detect" when the form got/lost focus (across applications), and not actually set it. Maybe I don't understand though.
Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.
Elroy:
I agree with your reading of OP's question.
Since I still use VB5 and there is no Validate event, that API allows one to Set the focus back to the last control they just left, and hence some workaround code, since part of his complaint was that LostFocus was Not firing.
I threw that API out there as "A POSSIBLE", instead of Subclassing, since no code was posted.
Regards.
Chances are, the reason the FORM didn't throw a "lost-focus" event, was because it didn't have it. (But, I understand windows, and know that once a program actually looses focus, it can ALSO loose the ability to react too. Assuming it DID have focus, and the event just wasn't getting handled by VB6 in time.)
The focus was probably, I assume, on a control within the form. It (the control that had focus) would be the thing which looses it, not the form. The form already "lost focus", once some other object "got focus".
If you wanted to see when the whole program/window "has focus", you would have to actively monitor it with this...
"GetForegroundWindow" function
Reason being... "Windows" don't have a focus, really. The "focus", is "active or inactive". (Active being "focused as the input program")
Or, you would have to monitor each item that can "get focus", in your program, and add code to the "lost focus" and "got focus" functions. Some code that set a value for when it has focus and looses focus. When all values are 0, then nothing "has focus" in your program, so the "window" has lost focus. If the value is > 1, than SOMETHING in the form/window has focus.
Last edited by ISAWHIM; Mar 27th, 2023 at 01:17 PM.