Hola:
He rehecho el código para ir deprisa he hecho mi propia adaptación, me funciona correctamente.
Código:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Controls, Forms, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
Index:integer;
listaclass:array[1..10] of TFormclass;
listpant:array[1..10] of TForm;
function Existe(Nombre: string): boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2, Unit3;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
//relleno los arrays con los forms y TForm
//creo que en tu caso son diferentes
listaclass[1]:=TForm2;
listaclass[2]:=TForm3;
listpant[1]:=Form2;
listpant[2]:=Form3;
end;
function TForm1.Existe(Nombre:string):boolean;
Var
i:integer;
begin
//busca en las ventanas creadas si existe la muestra
Result:=False;
For I:=Screen.FormCount-1 downto 0 do
if Screen.Forms[i].Name = Nombre then
begin
Result:=True;
Screen.Forms[i].Show;
break;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
Nombre:string;
begin
//muestra o crea la ventana con el nomre indicado en el edit1.text
Nombre:=Edit1.text;
if Existe(Nombre) then exit;
For i:=Low(listaclass) to High(listaclass) do
begin
if Assigned(listaclass[i]) and (listaclass[i].ClassName='T'+Nombre) then
begin
Application.CreateForm(listaclass[i],listpant[i]);
// listpant[i]:=listaclass[i].Create(Self); //no hay problema también sirve
Index := i;
listpant[Index].Show;
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//esto cierra la última que has abierto sólo la última
if Assigned(listpant[Index]) then FreeAndNil(listPant[Index]);
//if Assigned(listpant[Index]) then listpant[Index].Close; //no hay problema también sirve
//prefiero el close
end;
procedure TForm1.Button3Click(Sender: TObject);
var
i: integer;
Nombre:string;
begin
//Cierra la ventana indicada en el Edit1.text
Nombre:=Edit1.text;
For I:=Screen.FormCount-1 downto 0 do
if Screen.Forms[i].Name = Nombre then Screen.Forms[i].Close;
end;
end.