[Gambas-user] Check if a form is opened/show

Bruce bbruen at ...2308...
Sun Aug 12 00:19:22 CEST 2012


On Sat, 2012-08-11 at 13:47 +0200, Fabien Bodard wrote:
> Or simply:
> If FMain then
> 
> If fmain is not used then it is not instanciate so its value must be
> null... I think...
> 
> Another way can be to add a static value to fmain
> 
> 'in fmain
> 
> Static Private  $iCnt as integer
> 
> Property Read IsOpened as boolean
> 
> PUblic sub _New()
> 
> inc $iCnt
> 
> endif
> 
> Public sub Form_Close()
> 
>   dec $iCnt
> 
> end
> 
> 
> Static Public Function IsOpened_Read() as Boolean
> 
>   Return $iCnt >0
> 
> end
> 
> 
> 'so in form 2 you can call :
> 
> 
> if FMain.IsOpened then ...


However, remember that if a form has been opened and then closed
non-modally, the form object remains. It is not null, but invalid.
(http://gambasdoc.org/help/def/invalid?v3)

In that case, checking for either IsNull(myForm) or trying to reference
a symbol within the form will both result in an Invalid Object(29) error
(http://gambasdoc.org/help/error/iobject?v3).

Typically, this arises when a non-modal child form is shown (and closed)
several times during an execution or when trying to close any non-modal
child forms when the program is exiting.

In these cases, the solution is to set the Persistent property of the
child form, either in the designer or via myForm.Persistent=True.
That way, you can always check for IsNull(myForm) and then
myForm.Visible with success.

So my generalised algorithm for non-modal forms is:
1. Ensure that the form object is created with Persistent=true
2. Check for "form is shown" by
        If not IsNull(myForm)
          If MyForm.Visible then
            ...
hth
Bruce







More information about the User mailing list