The information in this article applies to:
SUMMARYThe LostFocus event in Microsoft Visual Basic is useful when transferring control within an application, and you can use the form deactivate and activate events in versions 2.0 and 3.0 to see if the entire form has lost the focus. However, in version 1.0, no global routine exists to check for the entire form losing the focus. To check whether your version 1.0 application has lost the focus, periodically check the Windows API function GetActiveWindow in a Visual Basic timer event, as explained below. MORE INFORMATION
The only way that version 1.0 provides a check for loss of focus on a
form or control is by triggering the LostFocus event. A form does
support a LostFocus event; however, a form will only get focus if
there are no controls on that form. Focus goes to the controls on a
form, and when you click any other visible form, the control's
LostFocus procedure will be called. A control's LostFocus procedure
will also be called when another control on the form is activated. To
perform a routine that occurs only when the form loses focus requires
careful management of what generated a LostFocus event on each control
(such as setting a flag if another control's Click event was called).
Program ExampleThis single-form example will print "Lost Focus" on the form when you click a different window (such as when you click another program running in Windows).In Visual Basic, draw one timer control (Timer1) and one command button (Command1) on a single form (Form1). From the VB.EXE Code menu, choose View Code, and enter the following code for Form1, using (general) from the Object box, and (declarations) from the Procedure box:
From the Object box, choose Timer1, and from the Procedure box, choose Timer, and then put the following code in the Timer1_Timer procedure:
You must set FOCUS=TRUE in the Click event procedure of every control on the form, as follows: From the Object box, choose Command1, and from the Procedure box, choose Click, then put the following code in the Command1_Click procedure:
Double-click Form1 (at design time) and enter the following code for the Form_Click procedure:
You can now run the program. REFERENCES
"Programming Windows: the Microsoft Guide to Writing Applications for
Windows 3," Charles Petzold. Microsoft Press, 1990.
Additional query words: 2.00 3.00
Keywords : |
Last Reviewed: June 8, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |