如何为 delphi 中的组件正确调用自定义组件编辑器表单

How to properly invoke a custom component editor form for a component in delphi

我在调用 TComponentEditor 的 Edit 方法时遇到访问冲突 class:

    type
      TLBIWXDataGridEditor = class(TComponentEditor)
      public
        function GetVerbCount: Integer; override;
        function GetVerb(Index: Integer): string; override;
        procedure ExecuteVerb(Index: Integer); override;
        procedure Edit; override;
      end;

这里是重写的编辑方法:

    procedure TLBIWXDataGridEditor.Edit;
    var
      _DsgForm: TLBIWXDataGridDesigner;
    begin
      _DsgForm := TLBIWXDataGridDesigner(Application);
      try
        _DsgForm.DataGrid := TLBIWXDataGrid(Self.Component);
        _DsgForm.ShowModal;
      finally
        FreeAndNil(_DsgForm);
      end;
    end;

所有 TLBIWXDataGrid 属性只能在设计表单内更改,因为它没有任何已发布的属性。

在设计时通过双击组件调用 Edit 方法时,我得到 AV 或 IDE 突然崩溃。

我认为问题与其他被覆盖的方法无关,但这是它们的实现:

    procedure TLBIWXDataGridEditor.ExecuteVerb(Index: Integer);
    begin
      case Index of
        0: MessageDlg ('add info here', mtInformation, [mbOK], 0);
        1: Self.Edit;
      end;
    end;

    function TLBIWXDataGridEditor.GetVerb(Index: Integer): string;
    begin
      case Index of
        0: Result := '&About...';
        1: Result := '&Edit...';
      end;
    end;

    function TLBIWXDataGridEditor.GetVerbCount: Integer;
    begin
      result := 2;
    end;

我错过了什么?

这一行是错误的:

_DsgForm := TLBIWXDataGridDesigner(Application);

这是 类型转换Application 对象转换为 TLBIWXDataGridDesigner,这是行不通的。

改用这个:

_DsgForm := TLBIWXDataGridDesigner.Create(Application);

或者这个,因为你手动释放对话框,所以它不需要分配 Owner

_DsgForm := TLBIWXDataGridDesigner.Create(nil);