有一个已经设计好的窗体,被动态创建在另一个窗体的tab页中,创建的个数和tab页的个数相同。tab页的个数也是不定的。
如何能获取每个tab页中该窗体的各个控件的值。

解决方案 »

  1.   

    每个tab页中的窗体的控件名称都是一样的啊
      

  2.   

    var
      i,j,k: integer;
    begin
      for i:=0 to PageControl1.PageCount-1 do
      begin
        for j:=0 to PageControl1.Pages[i].ComponentCount-1 do
        begin
          if PageControl1.Pages[i].Components[j] is TXXForm then
          begin
            for k:=0 to PageControl1.Pages[i].Components[j].ComponentCount-1 do
            begin
              //...取Edit框的值
              if PageControl1.Pages[i].Components[j].Components[k] is TEdit then
              begin
                ShowMessage(TEdit(PageControl1.Pages[i].Components[j].Components[k]).Text);
              end;
              //...取TComboBox框的值
              if PageControl1.Pages[i].Components[j].Components[k] is TComboBox then
              begin
                ShowMessage(TComboBox(PageControl1.Pages[i].Components[j].Components[k]).Text);
              end;
              //....
            end;
          end;
        end;
      end;
    end;
      

  3.   

    for i := 1 to Count do
    begin
          TabSheet := TRzTabSheet.Create(Self);
          with TabSheet do
          begin
            PageControl:=RzPageControl2;
            Caption:='第'+IntToStr(i)+'个结果集';
            Application.CreateForm(TResultForm, ResultForm);
            ResultForm.Dock(TabSheet,BoundsRect);
          end;
    end;
    程序中的代码如上,但是按照天行者老大的代码执行PageControl1.Pages[i].ComponentCount这个值是0,怪了,为什么会取不到ResultForm呢
      

  4.   

    建议你在创建Form时就有一个类来记录这个窗体,可以自己定义一个类,用于存储所有Form的信息,下面是我写的一个类,你可以借鉴一下. 
    还有一个建议,就是你应该把Form1..Form10都继续同一个父类,把他们共有的信息都放在父类里,这个在TFormList里取出信息的时候,可以统一用其父类进行强制转换. 
    unit FormList;interfaceuses
      Classes, Forms;type
      TFormList = class
      private
        FList: TStrings;
        function GetCount: integer;
      public
        constructor Create;
        destructor Destroy; override;    function Add(AKey: string; AForm: TForm): Integer;
        function Get(AKey: string): TForm; overload;
        function Get(AIndex: Integer): TForm; overload;
        function IndexOf(AKey: string): Integer;
        procedure Delete(AKey: string);
        procedure Clear;    property Count: Integer read GetCount;
      end;implementationconstructor TFormList.Create;
    begin
      FList := TStringList.Create;
    end;destructor TFormList.Destroy;
    begin
      FList.Free;
      inherited;
    end;function TFormList.Add(AKey: string; AForm: TForm): Integer;
    var
      iIndex: Integer;
    begin
      iIndex := IndexOf(AKey);
      if iIndex < 0 then
        Result := FList.AddObject(AKey, AForm)
      else
      begin
        FList.Objects[iIndex] := AForm;
        Result := iIndex;
      end;
    end;function TFormList.Get(AKey: string): TForm;
    var
      iIndex: Integer;
    begin
      iIndex := IndexOf(AKey);
      if iIndex >= 0 then
        Result := TForm(FList.Objects[iIndex])
      else
        Result := nil;
    end;function TFormList.Get(AIndex: Integer): TForm;
    begin
      if (AIndex > -1) and (AIndex < Count) then
        Result := TForm(FList.Objects[AIndex])
      else
        Result := nil;
    end;function TFormList.IndexOf(AKey: string): Integer;
    begin
      Result := FList.IndexOf(AKey);
    end;procedure TFormList.Delete(AKey: string);
    var
      iIndex: Integer;
    begin
      iIndex := IndexOf(AKey);
      if iIndex >= 0 then
        FList.Delete(iIndex);
    end;procedure TFormList.Clear;
    begin
      FList.Clear;
    end;    function TFormList.GetCount: integer;
    begin
      Result := FList.Count;
    end;end.
      

  5.   

    Application.CreateForm(TResultForm,   ResultForm); 
    这种动态创建Form的Parent属于Application,PageControl1.Pages[i].ComponentCount=0是正常,不等于零才是不正常,你应该在动态创建Form的时候,指定Form的Parent是PageControl1.Pages[i]就可以了。
      

  6.   

    Application.CreateForm(TResultForm,   ResultForm); 
    ResultForm.Parent := RzPageControl2.Pages[i-1]//i从1开始
    天行者,指定了之后还是一样:PageControl1.Pages[i].ComponentCount=0谢谢suihu,但是总觉得应该有更简单的方法,劳驾天行者再帮偶看看 
      

  7.   

    明天帮你试试。你的问题应该还是在于:
    Application.CreateForm(TResultForm,ResultForm);
      

  8.   

    我在 Form2 上放 Edit1和Edit2,以下运行结果可以显示 Edit1和Edit2,说明方法是可行。
    我是以一个静态的Page页为例,动态一样。
    procedure TForm1.Button1Click(Sender: TObject);
    var
      fm: TForm2;
      i,j,k: integer;
    begin
      fm := TForm2.Create(PageControl1.Pages[0]);//如果是动态创建的Page,此处应该是动态创建的Page实例
      fm.Parent := PageControl1.Pages[0];        //同上
      fm.Align := alClient;
      for i:=0 to PageControl1.Pages[0].ComponentCount-1 do
      begin
        for j:=0 to PageControl1.Pages[0].ComponentCount-1 do
        begin
          if PageControl1.Pages[0].Components[j] is TForm2 then
          begin
            for k :=0 to PageControl1.Pages[0].Components[j].ComponentCount-1 do
              ShowMessage(PageControl1.Pages[0].Components[j].Components[k].Name);
          end;
        end;
      end;
    end;