0

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

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

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

4

1 回答 1

0

这是一些我认为可以工作的未经测试的代码。让我们将您的组件称为 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;
于 2013-05-05T20:59:07.823 回答