【问题标题】:Get components of a form at design time在设计时获取表单的组件
【发布时间】:2013-04-29 08:44:48
【问题描述】:

我创建了一个组件,当在设计时双击它时,它会创建另一个表单。代码如下:

function TMyComponentTest1.Execute: Boolean;
var
  Form: TMyComponentTest1Form;
begin
  try
    Form := TMyComponentTest1Form.Create (nil);
    Form.ShowModal;
  finally
    Form.Free;
  end;
end;

在这个新表单中,我必须获取主要设计表单的组件,但我做不到,有人知道我该如何完成吗? 我也尝试用“self”创建,但是当我双击它时,delphi 崩溃了......

【问题讨论】:

  • Create 应该在您输入try 之前立即

标签: forms delphi components design-time


【解决方案1】:

这是一些我认为可以工作的未经测试的代码。让我们将您的组件称为 TMyComponent。您一定是在 TMyComponent 的设计时包中创建您的 TMyComponentTest1Form 作为组件编辑器和/或属性编辑器的一部分。

然后,尝试像这样创建 TMyComponentTest1Form:

function TMyComponentTest1.Execute: Boolean;
var
  aForm: TMyComponentTest1Form;
  OwnerForm: TForm;
  aMyComponent: TMyComponent;
begin
  {OwnerForm is your main design form}
  OwnerForm := nil;

  {Get your component on the main design form}
  aMyComponent := TMyComponent(GetComponent(0))

  {Make sure your component's owner is a TForm}
  if (aMyComponent.Owner is TForm) then
    OwnerForm := TForm(aMyComponent.Owner);

  {You problem may be solved by making component form owner the Application}
  aForm := TMyComponentTest1Form.Create(Application);
  try
    {
    Now you should be able iterate the components owned by OwnerForm
    right here. If you do not want to do it here, add a TForm property 
    to your component and assign OwnerForm to it.
    }
    aForm.ShowModal;
  finally
    aForm.Free;
  end;
end;

【讨论】:

    猜你喜欢
    • 2015-05-14
    • 1970-01-01
    • 2016-02-10
    • 2012-07-27
    • 2012-05-09
    • 2021-03-16
    • 2016-02-28
    • 2017-11-01
    • 1970-01-01
    相关资源
    最近更新 更多