【问题标题】:How can I access the controls of a form embedded in a page control?如何访问嵌入在页面控件中的表单的控件?
【发布时间】:2011-01-24 18:43:21
【问题描述】:

在 Form1 中,我有 PageControl。在运行时,我的程序会创建标签页。在每个 TabSheet 中,我创建 Form2。在 Form2 我有一个 Memo1 组件。如何在 Memo1 中添加文本?​​

【问题讨论】:

    标签: delphi tpagecontrol


    【解决方案1】:

    你可以这样做:

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

    【讨论】:

      【解决方案2】:

      如果我猜对了你在做什么,

      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;
      

      【讨论】:

      • 超级!!只需稍加修改,我就可以做我想做的事情:) 伟大的工作
      • 各位帮帮我。当我只使用一个 TabSheet 时,一切都很顺利,但是当我使用更多时,会出现错误“列表索引超出范围(1)”。有什么想法吗??
      • 列表索引错误可能是因为您使用无效索引访问 Pages 或 Controls 集合。像上面的 Sergs 示例一样循环访问控件。像这样循环浏览页面: for I := 0 to PageControl1.PageCount - 1 do begin (PageControl1.Pages[I].Controls[0] as TForm2).Memo1.Lines.Add('text');结束;
      【解决方案3】:

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

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

      【讨论】:

      • 我认为您的意思是将您的第一段作为对 Serg 答案的评论,将第二段作为对 Ville 答案的评论。这两段都不是问题的答案。
      【解决方案4】:

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

      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;
      

      希望这对其他人有帮助

      【讨论】:

        猜你喜欢
        • 2018-09-04
        • 1970-01-01
        • 1970-01-01
        • 2020-04-04
        • 2013-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-21
        相关资源
        最近更新 更多