Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Preventing multiple instances of a form
Message
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00018445
Message ID:
00018462
Views:
34
>>>Does anyone have a way of preventing multiple instances of a form without setting the Form WindowType property to 1-Modal?
>>
>>Use the code similar to:
>>
>>If type('oMyForm') # 'O' or isnull(oMyForm)
>> oMyForm = creatobject('MyForm')
>>endif
>>
>>
>>Well anything like that should work.
>>
>>Tom
>
>
> I am not calling the forms as classes. I am using DO FORM. Is their something I could put in the Init method?

I use that in my base form class Init()
It is based on Tasmanian Traders app.
LOCAL lnMenuNum, ;
      lAllowInstance, ;
      lcFormName
lAllowInstance=thisform.allowinstance()
if lAllowInstance
	*-- Add form's caption to the menu
	thisform.AddToMenu()
else
  lcFormName = thisform.Name
  ACTIVATE Window &lcFormName && if already exist , just switch to it
    	
endif

return lAllowInstance
Base form has property nInstancesAllowed for the number of allowed instances (default=1).
Form's AllowInstance method:
LOCAL lnNumberAllowed,;
      lnCount,;
      lnTally,;
      lcSCX,;
      llOK

store 0 to lnCount,lnTally
lnNumberAllowed=thisform.nInstancesAllowed

** Fetch the SCX file of this formset (or form)
lcSCX=sys(1271,THIS)
llok=.t.
	lnNumberAllowed=int(lnNumberAllowed)
	for lncount=1 to _Screen.FormCount
		if UPPER(_screen.forms(lncount).BaseClass)='FORM'
			if sys(1271,_screen.forms(lncount))=lcSCX ;
			and thisform.name=_screen.forms(lncount).name
			   lntally=lntally+1
			endif
		endif   
	endfor
	if lnTally>thNumberAllowed
*optional
*		=messagebox('Only';
*		+alltrim(str(lnNumberAllowed));
*		+' instance(s) is(are) permitted.',16,;
*		'Limited Instance Form')
		llok=.f.
	endif	
return llok
Also it uses form's AddToMenu method:

*-- Add the form's caption to the Window menu popup
LOCAL lnBar

IF TYPE("oApp") == "O" && I have application object
*-- Find the next available bar number
IF CNTBAR("Window") = 0 OR ;
GETBAR("Window", CNTBAR("Window")) < 0 && At a Fox system BAR
lnBar = CNTBAR("Window") + 1
ELSE
lnBar = GETBAR("Window", CNTBAR("Window")) + 1
ENDIF

DEFINE BAR lnBar OF Window PROMPT thisform.caption AFTER _MLAST
lcFormName = thisform.Name
ON SELECTION BAR lnBar OF Window ACTIVATE Window &lcFormName
ENDIF


HTH,

Nick
Nick Neklioudov
Universal Thread Consultant
3 times Microsoft MVP - Visual FoxPro

"I have not failed. I've just found 10,000 ways that don't work." - Thomas Edison
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform