How to Open / Close Multiple Forms from a Main Form

-2

I have a FormPrincipal which has a Tmenu with Tactionlist , and from this form I want to call several other Forms, according to the menu items.

In the main form, I have a Panel and I want the other forms to open inside this panel.

I adjusted the code below, after help from other StackOverflow colleagues in English, and it worked:

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

But now I need to know which form is active in the Panel so I can close it once I close the other, ie I need to modify the above routine:

procedure TFormPrincipal.AbreFormBancoExecute(Sender: TObject);
begin   

  // identificar qual form está ativo dentro do FormPrincipal
  // fechar este form e em seguida rodar o código abaixo  
  Formbanco := Tformbanco.Create(Self);
  Formbanco.Parent := PanelCorpo;
  Formbanco.Align := alclient;
  Formbanco.BorderIcons := [];
  Formbanco.BorderStyle := bsNone;
  Formbanco.Show;
end;

Is this a good approach for handling Forms calls from a correct main form?

. Adjust the code to get the form currently open . At Formbanco I put the following:

procedure TFormbanco.FormShow(Sender: TObject);
begin     
  Edit1.text := Screen.Activeform.Name;  // não mostra Formbanco !!  
                                             // só mostra FormPrincipal !!
end;
    
asked by anonymous 17.09.2017 / 23:47

2 answers

1

Consider this example:

procedure TForm4.Button1Click(Sender: TObject);
begin
  vForm1 := TForm.Create(Self);
  vForm1.Parent := Panel1;
  vForm1.Width := 100;
  vForm1.Align := alLeft;
  vForm1.Name := 'Form1Teste1';
  vForm1.Show;
end;

procedure TForm4.Button2Click(Sender: TObject);
begin
  vForm2 := TForm.Create(Self);
  vForm2.Parent := Panel1;
  vForm2.Width := 100;
  vForm2.Align := alLeft;
  vForm2.Name := 'Form1Teste2';
  vForm2.Show;
end;

procedure TForm4.Button3Click(Sender: TObject);
begin
  if (Assigned(vForm1)) then
  begin
    ShowMessage('1 esta criado');
  end;

  if (Assigned(vForm2)) then
  begin
    ShowMessage('2 esta criado');
  end;
end;

Being vForm1 and vForm2, respectively, of type TForm that you would replace with your own forms!

if Assigned() Tests whether an object was signed, that is, whether the creation occurred, if it exists. Result is a Boolean.

You can also test if the object is different from nil which would also work. In case you would create a function to validate if the form was already created, this would avoid the redundancy of code that I left in the example (Button3Click)!

    
18.09.2017 / 13:10
1

Junior and James, I made an adjustment to the code and got the desired behavior. However, the CloseActiveForm (Forname: String) procedure, which takes the form name and executes a FreeAndNil (form) in the corresponding form is very hard-coded! Note that in the code to get the form to be closed it has several IFs. I think there should be a better way and get the form (variable form). Can you help me with this?

The Program There are 3 forms being the main form1 that will call form2 and form3 that will be opened in a panel in form1. As soon as form2 or form3 is opened, I show the form name open and I have a Check Active button that returns Screen.ActiveForm.Name

Form1 - Principal ...

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
        //    Showmessage('Form2 is  Nil');
        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
          //  Showmessage('Form3 is Nil');
        end;
end;


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

Form2 (same form3)

... 
   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;

    // a propriedade Screen.ActiveForm.Name se for usada trará
    // sempre o nome do Form1 - principal, porque este forms aqui
    // está com o Parent setado omo Painel1 do Forms 1 !!
    end;

    end.

Program Screens

    
20.09.2017 / 17:28