首页 > 解决方案 > 如何在delphi中为组件正确调用自定义组件编辑器表单

问题描述

调用 TComponentEditor 类的 Edit 方法时出现访问冲突:

    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;

我错过了什么?

标签: delphicomponentscustom-controls

解决方案


这一行是错误的:

_DsgForm := TLBIWXDataGridDesigner(Application);

它将对象类型转换Applicationa TLBIWXDataGridDesigner,这是行不通的。

改用这个:

_DsgForm := TLBIWXDataGridDesigner.Create(Application);

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

_DsgForm := TLBIWXDataGridDesigner.Create(nil);

推荐阅读