PRB: GotFocus Event Fails If MsgBox Invoked in LostFocus Event

Last reviewed: May 16, 1996
Article ID: Q85856
The information in this article applies to:
  • Enterprise Edition of Microsoft Visual Basic, version 4.0a, for Windows
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0

SYMPTOMS

Invoking a message box from a control's LostFocus event will prevent the GotFocus event of the next selected control from executing.

CAUSE

This happens because the GotFocus event is not executed. Removing the message box from the control's LostFocus will allow the GotFocus event to execute as expected.

WORKAROUND

To work around the problem, set a flag in the control's LostFocus event procedure. Then call a generic test routine from the next control's GotFocus event, as demonstrated in the following example:

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Create the following controls and properties for Form1:

       Control     Name     Property Setting
       ----------------------------------
       Text Box    Text1    TabIndex = 0
       Text Box    Text2    TabIndex = 1
       Text Box    Text3    TabIndex = 2
    
    

  3. Add the following code to the general Declarations section of Form1:

    Dim LastControl As Control Dim CurrControl As Control

       Sub CheckLostFocus ()
    
         If (LastControl.Tag = "True") Then
            X%=MsgBox("Is the value OK ?", 36, LastControl + " has Lost Focus")
    
            If X% = 6 Then    'if YES
               LastControl.Tag = ""
               CurrControl.SetFocus
            Else
               LastControl.SetFocus
            End If
         End If
    
       End Sub
    
       Sub Form_Load ()
          Set LastControl = Text1     'set to the first editable control
       End Sub
    
    
The instructions 4 through 5 apply to EACH of the 3 Text Boxes:

  1. Add the following code to the LostFocus event for EACH Text control:

       Sub Text1_LostFocus ()
          Set LastControl = Text1
       End Sub
    
       Sub Text2_LostFocus ()
          Set LastControl = Text2
       End Sub
    
       Sub Text3_LostFocus ()
          Set LastControl = Text3
       End Sub
    
    

  2. Add the following code to the GotFocus event of EACH Text control:

       Sub Text1_GotFocus ()
          Set CurrControl = Text1
          CheckLostFocus
       End Sub
    
       Sub Text2_GotFocus ()
          Set CurrControl = Text2
          CheckLostFocus
       End Sub
    
       Sub Text3_GotFocus ()
          Set CurrControl = Text3
          CheckLostFocus
       End Sub
    
    

  3. Add the following code to the Change event of EACH Text control:

       Sub Text1_Change ()
          Text1.Tag = "True"
       End Sub
    
       Sub Text2_Change ()
          Text2.Tag = "True"
       End Sub
    
       Sub Text3_Change ()
          Text3.Tag = "True"
       End Sub
    
    

  4. Press F5 to run the program.

Now, both message boxes should appear as expected when the focus is changed by using the TAB key or by clicking the next text box.

STATUS

This behavior is by design. It is a limitation of Visual Basic's MsgBox statement.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Create the following controls and properties for Form1:

       Control     Name     Property Setting
       ---------------------------------------
       Text Box    Text1    TabIndex = 0
       Text Box    Text2    TabIndex = 1
    
    

  3. Add the following code to the Text1_LostFocus event procedure:

       Sub Text1_LostFocus ()
          MsgBox "Text1 has Lost the Focus"
       End Sub
    
    

  4. Add the following code to the Text2_GotFocus event procedure:

       Sub Text2_GotFocus ()
          MsgBox "Text2 has Received the Focus"
       End Sub
    
    

  5. Press F5 to run the program.

Notice that when you click the second text box (Text2), the message box specified in the GotFocus event fails to display. This also occurs if you try to tab between text boxes or set up labels and quick keys.


Additional reference words: 1.00 2.00 3.00 4.00a
KBCategory: kbprg kbcode kbprb
KBSubcategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: May 16, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.