Microsoft KB Archive/85856

= PRB: GotFocus Event Fails If MsgBox Invoked in LostFocus Event =

Article ID: 85856

Article Last Modified on 2/11/2005

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q85856





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



CAUSE
This behavior occurs because the GotFocus event is not executed. Removing the message box from the control's LostFocus allows the GotFocus event to execute as expected.



RESOLUTION
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:  Start a new project in Visual Basic. Form1 is created by default.  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   Add the following code to the general Declarations section of Form1: Dim LastControl As Control Dim CurrControl As Control

Sub CheckLostFocus

If (LastControl.Tag = &quot;True&quot;) Then X%=MsgBox(&quot;Is the value OK ?&quot;, 36, LastControl + &quot; has Lost Focus&quot;)

If X% = 6 Then   'if YES LastControl.Tag = &quot;&quot; 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:

  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   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 </li>  Add the following code to the Change event of EACH Text control: Sub Text1_Change Text1.Tag = &quot;True&quot; End Sub

Sub Text2_Change Text2.Tag = &quot;True&quot; End Sub

Sub Text3_Change Text3.Tag = &quot;True&quot; End Sub </li> Press the F5 key to run the program. Note that both message boxes should appear as expected when the focus is changed by using the TAB key or by clicking the next text box.</li></ol>

<div class="status_section">

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

<div class="moreinformation_section">

Steps to Reproduce Behavior
<ol> Start a new project in Visual Basic. Form1 is created by default.</li>  Create the following controls and properties for Form1: <pre class="fixed_text">  Control     Name     Property Setting ---  Text Box    Text1    TabIndex = 0 Text Box   Text2    TabIndex = 1 </li>  Add the following code to the Text1_LostFocus event procedure: Sub Text1_LostFocus MsgBox &quot;Text1 has Lost the Focus&quot; End Sub </li>  Add the following code to the Text2_GotFocus event procedure: Sub Text2_GotFocus MsgBox &quot;Text2 has Received the Focus&quot; End Sub </li> Press the F5 key to run the program.NOTE: 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.</li></ol>

Keywords: kbprb kbctrl KB85856

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.