Microsoft KB Archive/149489

= FIX: GotFocus Event Not Executed in Sub Main and SetFocus =

Article ID: 149489

Article Last Modified on 6/28/2004

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q149489



SYMPTOMS
The GotFocus Event fails to execute when the form is displayed through a Sub Main event and the SetFocus event is executed.



RESOLUTION
Execute the DoEvents method after the SetFocus event.



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug has been fixed in Visual Basic 5.0.



Steps to Reproduce Behavior
 Start 16-bit or 32-bit Visual Basic 4.0, or if it is already running, click New Project on the File menu. Add a TextBox control to the Form1 form.  On the Insert menu, click Module to create a module. Copy the following code to the Module1 Code window: Option Explicit

Sub Main Load Form1 Stop End Sub   Copy the following code to the Code window of the Form1 form: Option Explicit

Private Sub Form_Load Me.Show Text1.SetFocus 'Uncomment the next line for Workaround 'DoEvents End Sub

Private Sub Text1_GotFocus Text1.Text = "GotFocus" End Sub  On the Tools menu, click Options to display the Options dialog box. Click the Project tab and set Sub Main as the Startup Form. Step through the application by pressing F8. Note that after the SetFocus event, the GotFocus event is skipped. If the DoEvents statement is uncommented, the GotFocus event will be fired if the application is not run in debug mode. If F8 is used to step through each line of code, the GotFocus event will still not be fired.</li></ol>

Additional query words: SetFocus GotFocus Module kbVBp400bug kbVBp500fix kbVBp kbdsd kbDSupport

Keywords: kbbug kbfix kbvbp500fix KB149489

-

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

© Microsoft Corporation. All rights reserved.