我在button1Click下建了n个窗体,现在我想遍历这此窗体对它进行设置,我怎样才能找到它呢。
用component[i].ClassName=TForm1找不到。
我现在是在新建一个窗体时用数组保存它的指针,但觉得麻烦。有没有更好的方法,请高手指教。

解决方案 »

  1.   

    我实现如下:但打开3个窗口,先关第二个现关第三个时出错。其它不出错误。
    unit Main;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Menus, ComCtrls,Unit2, StdCtrls;type
      TForm1 = class(TForm)
        MainMenu1: TMainMenu;
        File1: TMenuItem;
        Main1: TMenuItem;
        Sub1: TMenuItem;
        StatusBar1: TStatusBar;
        procedure FormCreate(Sender: TObject);
        procedure Main1Click(Sender: TObject);
        procedure Sub1Click(Sender: TObject);
      private
        { Private declarations }
        SubMain:TForm2;
      public
        { Public declarations }
        SubCount:integer;
        SubWindows:array [0..20] of TControl;  //最多打开21个子窗口
        procedure SetWindows(WindowCount,SubHeight,SubWidth:integer); //设置子窗口
        procedure SetLeftWindows();//设置左边主窗口
        procedure DelSubWindows(DelWindow:TControl); //删除子窗口
      end;var
      Form1: TForm1;implementationuses  Unit3;{$R *.dfm}procedure TForm1.DelSubWindows(DelWindow:TControl);
    var
      i,j:integer;
    begin
      DelWindow.Free;
      SubCount:=SubCount-1;
      if DelWindow.Top<>0 then
        i:=Round(DelWindow.Top/(ClientHeight/(SubCount+1)))
      else
        i:=0;
      for j:=i to SubCount do
      begin
        SubWindows[j]:=SubWindows[j+1];
      end;
      if SubCount<>0 then
        SetWindows(SubCount,Round((ClientHeight-StatusBar1.Height-4)/SubCount)-1
          ,Round(ClientWidth/3)-5);
    end;procedure TForm1.SetLeftWindows();
    begin
      if SubCount>0 then
        SubMain.Width:=ClientWidth-4
      else
        SubMain.Width:=Round(ClientWidth*2/3);
    end;procedure TForm1.SetWindows(WindowCount,SubHeight,SubWidth:integer);
    var
      i,j:integer;
    begin
      j:=0;
      for i:=0 to WindowCount-1 do
      begin
        SubWindows[i].Top:=j;
        SubWindows[i].Left:=Round(ClientWidth*2/3);
        SubWindows[i].Width:=SubWidth;
        SubWindows[i].Height:=SubHeight;
        j:=j+SubHeight;
      end;
      {for i:=0 to SubCount-1 do
      begin
       memo1.Lines.Add(SubWindows[i].Name);
      end;
      }
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      SubCount:=0;
    end;procedure TForm1.Main1Click(Sender: TObject);
    begin
      SubMain:=TForm2.Create(nil);
      SubMain.Top:=0;
      SubMain.Left:=0;
      SubMain.Height:=ClientHeight-StatusBar1.Height-4;
      SetLeftWindows();
      SubMain.Show;
    end;procedure TForm1.Sub1Click(Sender: TObject);
    var
      sub:TForm3;
    begin
      sub:=TForm3.Create(nil);
      SubWindows[subCount]:=Sub;
      inc(subCount);
      SetLeftWindows();
      SetWindows(SubCount,Round((ClientHeight-StatusBar1.Height-4)/SubCount)-1
        ,Round(ClientWidth/3.0)-5);
      Sub.Show;
    end;end.
      

  2.   

    var i:integer;
    begin
      if application.FindComponent('Form1')  is  TForm1 then showmessage('1');
    for i:=0  to application.ComponentCount-1 do
      if application.Components[i].ClassType=TForm1 then showmessage('2');
    end;
      

  3.   

    谢谢你的回叠,我一开始也是这么想的,但是没有用的,在程序运行时动态建的窗体,是找不到的。可以试试:
    fm:=Tform1.create(self)
    for i:= to ComponentCount-1 do
    beginend;
      

  4.   

    fm:=Tform1.create(self)
    ------------------------------>改
    fm:=Tform1.create(application)
    for i:=0  to application.ComponentCount-1 do
      if application.Components[i].ClassType=TForm1 then showmessage('2');
    end;
      

  5.   

    或者
    fm:=Tform1.create(self)
    fm.name='Form1';
    if application.FindComponent('Form1')  is  TForm1 then showmessage('1');
      

  6.   

    fm:=Tform1.create(self)
    for i:= to self.ComponentCount-1 do
    beginend;
    我这样写为什么会不行呢,
      

  7.   

    没有问题啊procedure TForm1.Button1Click(Sender: TObject);
    var ff: Tform2;
        i:integer;
    begin
        ff:=Tform2.Create(self);
        for i:=0 to self.ComponentCount-1 do
         if self.Components[i].ClassType=Tform2 then showmessage('');
    end;