寫為一個過程﹕
procedure TForm1.openchild(AForm: TForm;AFormClass: TFormClass;childcaption:string);
var
  i:integer;
begin
  for i:=0 to application.ComponentCount-1 do
  begin
    if (application.Components[i] as tform).Caption=childcaption then
    begin
      (application.Components[i] as tform).Show;
      exit;
    end
    else begin
      aform:=AFormClass.create(self);
      aform.show;
    end;  end;
end;調用﹕
rocedure TForm1.Button1Click(Sender: TObject);
begin
  openchild(form2,tform2,'form2');
end;報錯﹕  invalid class typecast問﹕如何解決

解决方案 »

  1.   

    1 if (application.Components[i] as tform).Caption=childcaption then
    改为
    if (application.Components[i] is AFormClass) then
      if (application.Components[i] as tform).Caption=childcaption then
    2 将
    aform:=AFormClass.create(self);
    aform.show;
    放到循环外
      

  2.   

    if (Application.Components[i] is tform) then加在
    if (application.Components[i] as tform).Caption=childcaption then前面有些这种代码的必要吗?
      

  3.   

    to: firetoucher
    代碼改后如下﹕(結果出現情況﹕每單擊一次就打開一個子窗口)
    procedure TForm1.Button1Click(Sender: TObject);
    var
      i,j:integer;
    begin
    showmessage(inttostr(form1.MDIChildCount));
    i:=form1.MDIChildCount;
    if i>0 then
    begin
      for j:=0 to i-1 do
      begin
        if form1.MDIChildren[i] is tform2 then
        begin
          form1.MDIChildren[i].Show;
          exit;
        end
        else
        begin
          form2:=tform2.Create(self);
          form2.Show;
        end;
      end;
    end
    else begin
      form2:=tform2.Create(self);
          form2.Show;
    end;
      

  4.   

    又看了一遍,需要的是加一句
    procedure TForm1.openchild(AForm: TForm;AFormClass: TFormClass;childcaption:string);
    var
      i:integer;
    begin
      for i:=0 to application.ComponentCount-1 do
      begin
    if (application.Components[i] is AFormClass) then//加这一句。
        if (application.Components[i] as tform).Caption=childcaption then
        begin
          (application.Components[i] as tform).Show;
          exit;
        end
        else begin
          aform:=AFormClass.create(self);
          aform.show;
        end;  end;
    end;
      

  5.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        procedure openchild(AForm: TForm;AFormClass: TFormClass;childcaption:string);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses Unit2;{$R *.dfm}
    procedure TForm1.openchild(AForm: TForm;AFormClass: TFormClass;childcaption:string);
    var
      i:integer;
    begin
      for i:=0 to application.ComponentCount-1 do
      begin
        if (application.Components[i] is AFormClass) then//¼ÓÕâÒ»¾ä¡£
        if (application.Components[i] as tform).Caption=childcaption then
        begin
          (application.Components[i] as tform).Show;
          exit;
        end
        else begin
          aform:=AFormClass.create(self);
          aform.show;
        end;  end;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      openchild(Form2,TForm2,'Form2');
    end;end.form1代码