主窗体form1:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  Tfrm=class of TForm;
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure Showfrm(Frmclass:Tfrm; FrmValue:TForm);
  end;var
  Form1: TForm1;implementation
     uses unit2,unit3,unit4;
   const
      ArrFrm:array[0..2] of Tfrm=(Tform2,Tform3,Tform4);
{$R *.dfm}
procedure TForm1.Showfrm(Frmclass:Tfrm; FrmValue:TForm);  
begin
      if assigned(FrmValue) then
                begin
               showmessage('存在');
               FrmValue.ShowModal;
                  end
               else
               begin
      FrmValue:=Frmclass.Create(application);
      try
      FrmValue.Showmodal;
      finally
         FrmValue.Free;   (1)
         FrmValue:=nil;   (2)
      end;
      end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
showfrm(Tfrm(ArrFrm[0]),form2);             //问题是怎么把form2,form3,form4用数组来调用
end;                                        end.   (1)与(2)调换一下才不会出现:FrmValue从来没有使用的delphi编译器的提示,但能顺利执行.

解决方案 »

  1.   

    恩 思路還可以。。
    showfrm(Tfrm(ArrFrm[0]),form2);             //问题是怎么把form2,form3,form4用数
    你可以再定義一個數組 保存form2.form3..與 
    ArrFrm:array[0..2] of Tfrm=(Tform2,Tform3,Tform4);中類型要一一對應 不然會出錯的。
      

  2.   

    beyondtkl(大龙驹)老师:   我正是想把引用变量 form2、form3、form4用数组来表示。因为这样,我可以不理会以后的一些变动,只调换枚举值,不知道怎么写?
      

  3.   

    直接用一个就行了,没必要用数组了,反正你用完了就释放了type
      Tfrm=class of TForm;
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        procedure Showfrm(FrmClass:TFrm;FrmValue: TForm);
      end;var
      Form1: TForm1;
      VarForm:Tform;Const
      ArrFrm:array[0..2] of Tfrm=(Tform2,Tform3,Tform4);implementation{$R *.dfm}{ TForm1 }procedure TForm1.Showfrm(FrmClass:TFrm;FrmValue: TForm);
    begin
       if assigned(FrmValue) then
       begin
         showmessage('´æÔÚ');
         FrmValue.ShowModal;
       end
       else
         begin
          FrmValue:=FrmClass.Create(application);
          try
            FrmValue.Showmodal;
          finally
             Freeandnil(FrmValue);
         end;
       end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      showfrm(ArrFrm[0],VarForm);
    end;
      

  4.   

    俺又修改的:主窗体form1:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      Tfrm=class of TForm;
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        procedure Showfrm(Frmclass:Tfrm; FrmValue:TForm);
      end;var
      Form1: TForm1;implementation
         uses unit2,unit3,unit4;
       const
          ArrFrm:array[0..2] of Tfrm=(Tform2,Tform3,Tform4);
    {$R *.dfm}
    procedure TForm1.Showfrm(Frmclass:Tfrm; FrmValue:TForm);  
    begin
       if not  assigned(FrmValue) then
          FrmValue:=Frmclass.Create(application);
          try
          FrmValue.Showmodal;
          finally
        
             FreeAndNil(FrmValu);
          end;
          end;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    showfrm(Tfrm(ArrFrm[0]),form2);             //问题是怎么把form2,form3,form4用数组来调用
    end;                                        end.