简体   繁体   中英

How to know which Form is opened and how to close it?

I have a TAction.OnExecute event handler triggered from my main form, FormPrincipal , which opens other Forms.

procedure TFormPrincipal.AbreFormBancoExecute(Sender: TObject);
begin
   Formbanco              := Tformbanco.Create(self);
   Formbanco.Parent       := PanelCorpo;
   Formbanco.Align        := alclient;
   Formbanco.BorderIcons  := [];
   Formbanco.Show;
   Formbanco.BorderStyle  := bsNone;
   Formbanco.SetFocus;
end;

Once I'll have several forms, how to know which one is opened and how to close it, before triggering OnExecute to open another Form?

=========== Finally it is Working as I expected ======= The main form is form1 from which I call form2 and form3. In form1 I have a panel1 which is parent of form2 and form3. See form1 code : ... var Form1: TForm1;

implementation
{$R *.dfm}
uses unit2, unit3;

procedure Tform1.CloseActiveForm (Formname : string);
// Free memory allocated to the current form , set it to nil
// I'll have to find a better way to perform FreeanNil without
// use many IFs command
begin
     if Formname  = 'form2' then FreeAndnil(Form2) else
         if Formname = 'form3' then FreeandNil(Form3);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
       CloseActiveForm(Edit1.Text); //Edit1 has the current active form name
       if form2 = nil  then
       begin
              Application.CreateForm(Tform2,Form2);
              Form2.Parent  := Panel1;
              Form2.Align   := alclient;

              Form2.Show;
              Form2.BorderStyle :=  bsnone;
              Form2.SetFocus;
              Form2.OnActivate(Sender); //Method Show blocks Activate event
      end;    
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
       CloseActiveForm(Edit1.Text); //Edit1 has the current active form name
       if form3 = nil  then
        begin
              Application.CreateForm(Tform3,Form3);
              Form3.Parent  := Panel1;
              Form3.Align   := alclient;

              Form3.Show;
              Form3.BorderStyle := bsnone;
              Form3.SetFocus;
              Form3.OnActivate(Sender);  //Method Show blocks Activate event             
        end;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
   Edit1.Text := Screen.ActiveForm.Name;
end;
end.

2) Code in form2 and form3 (consider form3 is identical)

...

var
  Form2: TForm2;

implementation

{$R *.dfm}
uses unit1;

procedure TForm2.Button1Click(Sender: TObject);
begin
       Edit2.Text := Screen.ActiveForm.Name;
end;

procedure TForm2.FormActivate(Sender: TObject);
begin
     setfocus;
     Edit1.Text       := Form2.Name;
     Form1.Edit1.Text := Form2.Name; //set form name 

// the property Screen.ActiveForm.Name if used here, will always presents
// form1 name (main form) because this form2 is set to a parent panel 
// in form1
end;  

end.

在此处输入图片说明

在此处输入图片说明

Destroy the form if it exist and create a new instance of it.

procedure TFormPrincipal.AbreFormBancoExecute(Sender: TObject);

  procedure CreateFormbanco;
    begin
    Formbanco               := TFormbanco.Create(self);
    Formbanco.Parent       := PanelCorpo;
    Formbanco.Align        := alclient;
    Formbanco.BorderIcons  := [];
    Formbanco.BorderStyle  := bsNone;
    Formbanco.Show;
    Formbanco.SetFocus;
    Formbanco.OnDestroy    := FormDestroyEvent;
    end;

begin
if not Assigned(Formbanco) then
  begin
  CreateFormbanco;
  end
else
  begin
  Formbanco.Destroy;
  CreateFormbanco;
  end;

procedure TFormPrincipal.FormDestroyEvent(Sender: TObject);
begin
  Formbanco := nil;
end;

This code will check if Formbanco existed, if so it will destroy it and create a new instance of it otherwise it will create a new one.

Edit: create different forms and use the code above, just change Formbanco and TFormbanco to their respected new form name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM