Search code examples
delphitpagecontrol

How can I access the controls of a form embedded in a page control?


In Form1 I have PageControl. At run time my program creates tab sheets. In each TabSheet I create Form2. In Form2 I have a Memo1 component. How can I add text to Memo1?


Solution

  • If I get right what are you doing,

    procedure TForm1.Button1Click(Sender: TObject);
    var
      View: TForm;
      Memo1, Memo2: TMemo;
      Page: TTabSheet;
      I: Integer;
    
    begin
      View:= TForm2.Create(Form1);
      View.Parent:= PageControl1.Pages[0];
      View.Visible:= True;
      View:= TForm2.Create(Form1);
      View.Parent:= PageControl1.Pages[1];
      View.Visible:= True;
    // find the first memo:
      Page:= PageControl1.Pages[0];
      Memo1:= nil;
      for I:= 0 to Page.ControlCount - 1 do begin
        if Page.Controls[I] is TForm2 then begin
          Memo1:= TForm2(Page.Controls[I]).Memo1;
          Break;
        end;
      end;
      Page:= PageControl1.Pages[1];
    // find the second memo:
      Memo2:= nil;
      for I:= 0 to Page.ControlCount - 1 do begin
        if Page.Controls[I] is TForm2 then begin
          Memo2:= TForm2(Page.Controls[I]).Memo1;
          Break;
        end;
      end;
      if Assigned(Memo1) then Memo1.Lines.Add('First Memo');
      if Assigned(Memo2) then Memo2.Lines.Add('Second Memo');
    end;