Board index » delphi » Close All Modal Forms except menu

Close All Modal Forms except menu

D2.0 CS, W95
My application only uses modal forms, and in some cases I have to close
many forms to return to the main menu form.
Is there a way that I could close the current form and also all the
others. I thought about setting a variable such as CloseAll:=true;
but where would I test for this when control returns to a previous modal
form.
Thanks for any suggestions
David Carle

 

Re:Close All Modal Forms except menu


Quote
On Sat, 07 Feb 1998 17:30:50 -0800, David Carle <d...@clara.net> wrote:
>D2.0 CS, W95
>My application only uses modal forms, and in some cases I have to close
>many forms to return to the main menu form.
>Is there a way that I could close the current form and also all the
>others. I thought about setting a variable such as CloseAll:=true;
>but where would I test for this when control returns to a previous modal
>form.
>Thanks for any suggestions
>David Carle

Hi David,

You could do this in various ways, let me suggest the following:

Create a general purpose unit (not associated with any form) :
unit Globals;
interface
var
   xbCloseAll: Boolean;
implementation
end.
and include it in the uses clause of your modal forms.

Now you set and test this as follows :

{Top unit in the chain of modal forms.
  This one should only close on an explicit
  request }
procedure TForm1.SomeEvent(Sender: TObject);
begin
        Form2.ShowModal;
        xbCloseAll := False;
        // Reset the flag so that you can
        // re-launch the modal forms
end;
{Middle unit in the chain.
  You may have a number of them...}
procedure TForm2.SomeEvent(Sender: TObject);
begin
        Form3.ShowModal;
        // Test here if some form down in the chain
        // has decided to collapse the chain
        if xbCloseAll
           then begin
        // Note: you may test here some conditions to
        // verify this form may be closed
                Close;
           end;
end;
{Bottom unit in the chain}
procedure TForm3.SomeEvent(Sender: TObject);
begin
        // Here we close this form and request
        // the whole chain to collapse
        xbCloseAll := True;
        Close;
end;

Pierre Gemis

Re:Close All Modal Forms except menu


David

Let me suggest another way. One that uses inheritence instead of a global
unit or variable.

Create a base form that has a property which when set to true first checks
to see if its 'owner' form is of the same type and if so sets its
'CloseAll' propterty to True and then closes itself.

i.e.
type
  TfrmBaseCloser = class(TForm)
  private
    _CloseAll : Boolean;
  protected
     procedure setCloseAll( doit : Boolean );
  published
    property CloseAll : Boolean read _CloseAll write setCloseAll;
  public
  end;

procedure TfrmBaseCloser.setCloseAll( doit : Boolean );
begin
    if doit then
       begin
       if self.owner is TfrmBaseCloser then
          TfrmBaseCloser(self.owner).closeall := True;
       self.close;
       end;
end;

Now derive all your actual working forms from this base form, all that is
except for the form you want to stay open.

i.e.

type
    TfrmModal : class( TfrmBaseCloser)
    .......

In your program you instantiate the working forms using the 'self' property
to setup the ownership chain.

ie

procedure TfrmModal.btnNewClick(Sender: TObject);
var
   dlg : TfrmModal;
begin
        dlg := TfrmModal.create( self );
        dlg.Top := self.Top + 10;
        dlg.Left := self.Left + 10;
        dlg.showmodal;
end;

Now you can either close one of these forms with the standard 'close' or
close all the forms in the chain of ownership by setting CloseAll := True;

ie.

procedure TfrmModal.btnCloseMeClick(Sender: TObject);
begin
     self.close;
end;

procedure TfrmModal.btnCloseAllClick(Sender: TObject);
begin
     closeall := True;
end;

I have attached a little project that demostrates this in action.  You
might consider extending this so that in setCloseAll procedure you actually
post a message to the parent instead of setting a property directly but I
will leave this up to you.

HTH

Andy Jensen
        atjensen@bellatlantic

Pierre Gemis <sof...@redestb.es> wrote in article
<34def4c3.4229...@news.redestb.es>...

Quote
> On Sat, 07 Feb 1998 17:30:50 -0800, David Carle <d...@clara.net> wrote:

> >D2.0 CS, W95
> >My application only uses modal forms, and in some cases I have to close
> >many forms to return to the main menu form.
> >[snip]
> >Thanks for any suggestions
> >David Carle

> Hi David,

> You could do this in various ways, let me suggest the following:

> Create a general purpose unit (not associated with any form) :
> unit Globals;
> interface
> var
>    xbCloseAll: Boolean;
> implementation
> end.
> and include it in the uses clause of your modal forms.

>[snip]
> Pierre Gemis

  CloseAll.dpr
< 1K Download

  uBaseCloser.dfm
< 1K Download

  uBaseCloser.pas
< 1K Download

  uMain.dfm
< 1K Download

  uMain.pas
< 1K Download

  uModal.dfm
< 1K Download

  uModal.pas
< 1K Download

Re:Close All Modal Forms except menu


On 13 Feb 1998 02:22:14 GMT, "atjensen" <atjen...@bellatlantic.net>
wrote:

Quote
>This is a multi-part message in MIME format.

>------=_NextPart_000_01BD37FB.62ADD3C0
>Content-Type: text/plain; charset=ISO-8859-1
>Content-Transfer-Encoding: 7bit

>David

>Let me suggest another way. One that uses inheritence instead of a global
>unit or variable.

>[snip]

Indeed more elegant and OOP-minded.

Pierre Gemis

Other Threads