Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking opened form

Status
Not open for further replies.

jerold

Programmer
Aug 30, 2000
52
PH
How do I check if the a certain form is already
opened?
 
From the documentation (both VFP docs and Hacker's Guide), it appears that WEXIST() only works for user-defined and system windows, not for forms.

As an alternative, you could scan through the _screen.forms collection looking for the desired form. Something like this (I have not tested this, so beware):

IsFormRunning.PRG
Code:
lParameter tcFormName
for each oForm in _screen.forms
   if upper(oForm.Name) = upper(tcFormName)
      return .T.
   endif
endfor
return .F.
 
Robert

I would not want to dispute the oracle, but if you add a snippit such as:-

IF WEXIST([MAINFORM]) && Form name for known open user-defined form
[tab]glPublicVariable = .T.
ENDIF

you should find that glPublicVariable is .T.

Chris

 
Both examples are accurate, as long as the formname you are referring to is the form's .name property and not the physical .scx name.

DO FORM MyFormFileName NAME MyForm LINKED

WEXIST('MyFormFileName') && Returns .F.
WEXIST('MyForm') && Returns .F.
WEXIST(MyForm.Name) && Returns .T.

Just my .02
 
FoxDev spew forth:
From the documentation (both VFP docs and Hacker's Guide), it appears that WEXIST() only works for user-defined and system windows, not for forms.

I continue to stand wavering in the wind atop my original statement.

From the VFP on-line help:

WEXIST( ) returns true (.T.) if the user-defined window you specify has been created with DEFINE WINDOW; otherwise WEXIST( ) returns false (.F.).

From the Hacker's Guide to Visual FoxPro 6.0:

These functions return information about user-defined windows, system windows and toolbars. With forms, the WindowState and Visible properties provide much of the same information

I'm not saying WEXIST() won't work; I'm saying that, according to the documentation, it isn't intended to work.
 
Guys, its obvious. For forms it works always. Unless you open more than one instance of the same form simultaneously. WEXIST function also will return unpredictable results in some cases when you open/close more than one instance of the DIFFERENT forms that have the SAME name (thisform.name property).
When such details will be written in each help, we will have no time to read them. In such cases just more simple to write 'It will not work'.
Concerning the jerold's problem, following is code to check that some form is really opened.

PROCEDURE CheckFormOpened
LPARAMETERS pcFormName, pcFormCaption
* form name, form caption (optional,
* use it to identify forms that opened twice or more times)
* Samle of call:
* CheckFormOpened('MyFormName'[,'MyFormCaption'])
LOCAL nFormIndex, lcFormCaption
IF PARAMETERS() > 1
m.lcFormCaption = UPPER(ALLTRIM(m.pcFormCaption))
ELSE
m.lcFormCaption = ""
ENDIF
m.pcFormName = UPPER(m.pcFormName)
* loop through all screen forms
FOR m.nFormIndex = 1 TO _screen.FormCount
&& following IF may be removed when you're sure
&& this function will never be called from
&& places where form destroying is in progress,
&& this will speed up this function
IF TYPE('_screen.Forms(m.nFormIndex).BaseClass') == "C"
IF UPPER(_screen.Forms(m.nFormIndex).Name) == m.pcFormName
IF m.lcFormCaption == ""
RETURN .T.
ELSE
&& check also for caption
IF UPPER(ALLTRIM(_screen.Forms(m.nFormIndex).Name)) == m.lcFormCaption
RETURN .T.
ENDIF
ENDIF
ENDIF
ENDIF
ENDFOR
RETURN .F. && tried all forms - not found
 
If you use an 8 character DOS type filename for the .scx, and use the same name for the FORMNAME.Name property, you have one name for both situations, and thus WEXISTS() always works.

This is one of those quirky situations where it is better not to read the instructions!

Chris
 
Sorry, if I touch someone's ambitions here.
Is is quite not good for programming of large projects, indeed. Imagine, you will need to check all ~100 forms to verify that all forms have 8 character name and thisform.name match it. Quite bad for project management. It is much more simple to write routine like proposed and do not aware about length of .scx file name and leave form name for programmer that made this form (they may use thisform.name inside of his/her code).
The less limitation rules for project, the more easy project management, application will be more reliable. That is why I always divide VFP functionality to 'OLD' and 'NEW'. I use 'OLD' OONLY in case where it cannot be replaced by any 'NEW' functionality. So, I always use long file names for forms and never use WExist function even for user-defined windows (in VFP you may create object reference variable for user-defined window). This later have great benefit in project maintaining. And I very appreciate new style of VFP programming, MS guys made it very good to create projects more quickly and not aware about many details, that you need to aware in older FoxPro versions.
 
Volodymyr

The first paragraph of my last post was in response to Jon Hawkin's comment about as long as the formname you are referring to is the form's .name property and not the physical .scx name. and how to ensure accuracy.

The second paragraph refered to Robert's original statement concerning WEXIST(). I, and probably many others, have been using WEXIST() in blissful ignorance of the fact that it is not supposed to work!

There is no doubt that your routine is the proper way to approach the problem, but WEXIST() does provide a solution for simple applications.

Chris

 
Chris, I am not arguing against WEXIST(), and I agree with you that if it works now, it'll probably continue to work, especially for situations where you will only have one instance of a given form at a time. I'm all for the "if it works, use it" approach. I got away from using WEXIST because I found unpredictable results (does it return the physical file name, or the form.name, or the memvar name, or...) - or maybe I just couldn't keep it straight in my head what it would return for forms.

Like so many others, I was dragged into OOP kicking and screaming; but now that I am a convert, I always look first for an OOPish way of working and coding. After five years of slow, painful progress, _screen.forms() is now more intuitive to me than wexist().
 
Chris, I understand you. However, sometimes you will need to expand simple application to be more complex or enhanced. What you will do with WEXIST than? That is why I use always NEW way (OOPish way - copyright FoxDev :) even for simple application. In future I will have no problems to improve these applications.
 
Volodymyr

There is no argument over the correct approach, and I think jerold should be grateful to you and Robert for your contributions.

Undoubtedly WEXIST() will run out of breath once a project grows, and it does therefore make sense to structure it properly from the outset.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top