PRB: SetFocus During Form Load May Cause Illegal Function Call

ID: Q88477


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for Windows, versions 2.0, 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0


SUMMARY

Symptoms

Using the SetFocus method to set the focus to a specific control on a form during the form load event procedure may result in an "Illegal Function Call" error.

Cause

This error occurs because the form that the control is on is not yet visible.

Resolution

To prevent this error from occurring, execute Form.Show before executing the SetFocus method.


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 is created by default.


  2. Place a text box (Text1) on Form1.


  3. Add the following code to the Form_Load procedure for Form1:
    
       Sub Form_Load ()
          Text1.SetFocus
       End Sub 


  4. Press F5 to run the application. The line of code in the load event will be highlighted with the error "Illegal Function Call."


  5. If you show the form before executing SetFocus, the program will run as expected.
    
       Sub Form_Load ()
          Form1.Show
          Text1.SetFocus
       End Sub 


Additional query words: 2.00 3.00

Keywords :
Version :
Platform :
Issue type :


Last Reviewed: September 2, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.