Delphi 如何访问页面控件中嵌入的窗体控件?

Delphi 如何访问页面控件中嵌入的窗体控件?,delphi,tpagecontrol,Delphi,Tpagecontrol,在Form1中,我有PageControl。在运行时,我的程序创建选项卡页。在每个选项卡中,我创建Form2。在Form2中,我有一个Memo1组件。如何将文本添加到备忘录1?您可以执行以下操作: (PageControl1.Pages[0].Controls[0] as TForm2).Memo1.Lines.Add('text'); 如果我说对了你在干什么 procedure TForm1.Button1Click(Sender: TObject); var View: TForm;

在Form1中,我有PageControl。在运行时,我的程序创建选项卡页。在每个选项卡中,我创建Form2。在Form2中,我有一个Memo1组件。如何将文本添加到备忘录1?

您可以执行以下操作:

(PageControl1.Pages[0].Controls[0] as TForm2).Memo1.Lines.Add('text');

如果我说对了你在干什么

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;

我发现这段代码有一个大问题——Memo2的值将与Memo1完全相同,因为在搜索循环中没有区别。此外,如果此代码已完成,那么页面上除了表单之外什么也没有,根本没有理由进行搜索循环


VilleK的答案应该编译并运行,我看不出你在问什么。

所以,我在你的帮助下解决了我的问题。这是我的代码:

var
ID, I: integer;
Tekstas: string;
View: TForm2;
Memo: TMemo;
Page: TTabSheet;
begin 
...
   Page := PageControl.Pages[ID];
   for i := 0 to Page.ControlCount - 1 do
   begin
   (PageControl.Pages[ID].Controls[0] as TKomp_Forma).Memo.Lines.Add('['+TimeToStr(Time)+']'+Duom[ID].Vardas+': '+Tekstas);
   end;
end;

希望这对其他人有帮助

Super!!只要稍加修改,我就能做我想做的事:)优秀的工作人员帮我。当我只使用一个选项卡时,一切都很顺利,但当我使用更多选项卡时,会出现错误“列表索引超出范围(1)”。任何想法??列表索引错误可能是因为您使用无效索引访问页面或控件集合。像上面的Sergs示例中那样循环控制。循环遍历如下页面:对于I:=0到PageControl1.PageCount-1,确实要开始(PageControl1.pages[I]。控件[0]作为TForm2)。Memo1.Lines.Add('text');结束;我想你是想把第一段作为对塞格答案的评论,第二段作为对维尔答案的评论。这两段都不是问题的答案。