VB GotFocus Event Fails if MsgBox Invoked in LostFocus Event

ID Number: Q85856

1.00

WINDOWS

buglist1.00

Summary:

Invoking a message box from a control's LostFocus event will prevent

the GotFocus event of the next selected control from executing. This

problem is illustrated in the example below.

Microsoft has confirmed this to be a problem in Microsoft Visual Basic

programming system version 1.0 for Windows. We are researching this

problem and will post new information here as it becomes available.

More Information:

Steps to Reproduce Problem

--------------------------

1. Start Visual Basic, or from the File menu, choose New Project

(ALT, F, N) if Visual Basic is already running. Form1 will be

created by default.

2. Create the following controls for Form1:

Control CtlName 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 on the second text box (Text2), the message

box specified in the GotFocus event fails to display. This also

happens if you try to tab between text boxes or set up labels and

quick keys. This is because the GotFocus event is not executed.

Removing the message box from the Text1_LostFocus will allow the

Text1_GotFocus event to execute as expected.

Workaround

----------

As a workaround, 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 Visual Basic, or from the File menu, choose New Project

(ALT, F, N) if Visual Basic is already running. Form1 will be

created by default.

2. Create the following controls for Form1:

Control CtlName Property Setting

------- ------- ----------------

Text Box Text1 TabIndex = 0

Text Box Text2 TabIndex = 1

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

Form1:

Dim Text1LostFocus As Integer

Sub CheckLostFocus()

If Text1LostFocus Then

MsgBox "Text1 has Lost the Focus"

Text1LostFocus = 0

End If

End Sub

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

Sub Text1_LostFocus ()

Text1LostFocus = -1

End Sub

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

Sub Text1_GotFocus ()

Call CheckLostFocus

MsgBox "Text2 has Received the Focus"

End Sub

6. Press F5 to run the program.

Both message boxes should appear as expected when the focus is changed

by using the TAB key or clicking the mouse on the Text2 text box.

Additional reference words: 1.00