Microsoft KB Archive/130531

= How To Prevent Two Instances of a Form from Running =

Article ID: 130531

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q130531



SUMMARY
A form created in Visual FoxPro defaults to a multiple-instance form; that is, multiple instances of the form are allowed to run concurrently. This article shows by example how to create a form that does not allow another instance of itself to be created, which is how FoxPro version 2.x forms behave.



MORE INFORMATION
To prevent the creation of a second instance of a form, add a procedure to the Init event of the form to check all open forms for an instance of itself. Have the procedure return false (.F.) if an instance already exists. This will prevent Visual FoxPro from launching the second instance.

Step-by-Step Example
  Create a new form by typing the following in the Command window: CREATE FORM test  From the View menu, choose Data Environment. Add the Customer table from the \VFP\SAMPLES\DATA directory. Drag some fields from the Data Environment to the form. Right-click anywhere on the form to open the property sheet. In the property sheet, select the Methods Tab and open the Init event.  Type the following code in the Init Event: LOCAL i, lcWindName, llRetVal

* Initialize the variables. llRetval returns a .F. from the INIT if  * the form is already visible. Returning a .F. from any INIT causes * the object to not be created. * i contains the number of instances of the form. * lcWindName contains the name of the form you're looking at.

llRetVal = .T.  i = 0 lcWindName = WCHILD("",0) && The "" must be a NULL or empty string

* Loop through all open forms, and see if we have any that have the same * name as the one you want to open. (It exists in INIT but is not yet  * available.)

DO WHILE !EMPTY(lcWindName) IF lcWindName = UPPER(this.name) i = i + 1 IF i > 1 llRetVal = .F.           EXIT ENDIF ENDIF lcWindName = WCHILD("",1) && The "" must be a NULL or empty string ENDDO

RETURN llRetVal  Save and run the form.</ol>

Additional query words: VFoxWin

Keywords: kbhowto kbcode KB130531

-

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

© Microsoft Corporation. All rights reserved.