【问题标题】:Passing inherited frames as argument to a procedure将继承的帧作为参数传递给过程
【发布时间】:2020-01-02 10:30:13
【问题描述】:

我的主窗体中有一个 TPageControl,其中包含 N 个 TTabSheets,我用它来嵌入多个 TFrame 后代。 对于我创建了一个“TBaseFrame”的框架,我从中派生出我想在 TabSheets 中显示的各个框架,或多或少看起来像这样......

TBaseFrame = 类(TFrame)

  • TBaseFrameDescendant1 = 类(TBaseFrame)
  • TBaseFrameDescendant2 = 类(TBaseFrame)
  • TBaseFrameDescendantN = 类(TBaseFrame)

我遇到的问题是:我想创建一个过程,将我的任何 TBaseFrameDescendants 作为参数,创建给定的框架并将其显示在新的选项卡表中。我从这样的事情开始......

procedure CreateNewTabSheetAndFrame( What do I put here to accept any of my TBaseFrameDescendants? )
var
  TabSheet: TTabSheet;

begin
  TabSheet := TTabSheet.Create(MainPageControl);
  TabSheet.Caption := 'abc';
  TabSheet.PageControl := MainPageControl;

// Here I want to create the given TBaseFrameDescendant, set the Parent to the above TabSheet and so on   
end;

猜我这里的主要问题是如何设置我的程序,以便我可以传递从我的 TBaseFrame 派生的任何帧,以便我可以在程序中使用它,还是我在这里走错了方向?

【问题讨论】:

    标签: function oop delphi inheritance procedure


    【解决方案1】:

    您需要使用所谓的元类。

    type
      TBaseFrameClass = class of TBaseFrame;
    
    procedure TMainForm.CreateNewTabSheetAndFrame(FrameClass: TBaseFrameClass)
    var
      TabSheet: TTabSheet;
      Frame: TBaseFrame;
    begin
      TabSheet := TTabSheet.Create(Self);
      TabSheet.PageControl := MainPageControl;
      Frame := FrameClass.Create(Self);
      Frame.Parent := TabSheet;
    end;
    

    如果您在任何框架类中声明任何构造函数,请确保它们派生自TComponent 中引入的虚拟构造函数。为了通过元类实例化调用适当的派生构造函数,这是必要的。

    【讨论】:

    • 干杯大卫,几秒钟后就可以工作了,非常感谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 2019-09-25
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 2011-12-28
    相关资源
    最近更新 更多