Delphi TCollectionItem的子代中的事件

Delphi TCollectionItem的子代中的事件,delphi,events,tcollectionitem,Delphi,Events,Tcollectionitem,我编写了简单的代码(见下文):带有事件的TCollectionItem的后代。但是,当我在object inspector中单击“OnDone事件”时,我会收到以下消息: “无法为未命名组件创建方法” 这个代码有什么问题 unit MainComponent2; interface uses Windows, SysUtils, Classes; type TMyField = class(TCollectionItem) private FName: string;

我编写了简单的代码(见下文):带有事件的
TCollectionItem
的后代。但是,当我在object inspector中单击“OnDone事件”时,我会收到以下消息:

“无法为未命名组件创建方法”

这个代码有什么问题

unit MainComponent2;

interface

uses Windows, SysUtils, Classes;

type
  TMyField = class(TCollectionItem)
  private
    FName: string;
    FOnDone: TNotifyEvent;
    FText: string;
  protected
    function GetDisplayName : String; override;
  public
    constructor Create(ACollection: TCollection);override;
    function GetNamePath: string;override;
  published
    property Name: string read FName write FName;
    property Text: string read FText write FText;
    property OnDone: TNotifyEvent read FOnDone write FOnDone;
  end;

  TMyFields = class(TOwnedCollection)
  private
    function GetItem(Index: Integer): TMyField;
    procedure SetItem(Index: Integer; const Value: TMyField);
  protected
    procedure Update(Item: TmyField);reintroduce;
  public
    constructor Create(AOwner: TComponent);
    function Add: TMyField;
    function Insert(Index: Integer): TMyField;
    property Items[Index: Integer]: TMyField read GetItem write SetItem; default;
  end;


  TMainComponent2 = class(TComponent)
  private
    FMyFields: TMyFields;
    function GetItem(index: integer): TMyField;
    procedure SetItem(index: integer; const Value: TMyField);
    procedure SetMyFields(const Value: TMyFields);
  public
    constructor Create(AOwner: TComponent);override;
    destructor Destroy;override;

    property Items[index: integer]: TMyField read GetItem write SetItem;
  published
    property MyFields: TMyFields read FMyFields write SetMyFields;
  end;

procedure Register;

implementation

{ TMainComponent2 }

constructor TMainComponent2.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FMyFields := TMyFields.Create(AOwner);
end;

destructor TMainComponent2.Destroy;
begin
  FMyFields.Free;
  inherited;
end;

function TMainComponent2.GetItem(index: integer): TMyField;
begin
  result := FMyFields.Items[index] as TMyField;
end;

procedure TMainComponent2.SetItem(index: integer; const Value: TMyField);
begin
  FMyFields.Items[index] := Value;
end;

procedure TMainComponent2.SetMyFields(const Value: TMyFields);
begin
  FMyFields.Assign(Value);
end;

{ TMyField }

constructor TMyField.Create(ACollection: TCollection);
begin
  inherited Create(ACollection);
  FName := ClassName + IntToStr(ID);
end;

function TMyField.GetDisplayName: String;
begin
  result := FName;
end;

function TMyField.GetNamePath: string;
begin
  Result := Format('%s%d',['MyField', Index])
end;

{ TMyFields }

function TMyFields.Add: TMyField;
begin
  result := (inherited Add) as TMyField;
end;

constructor TMyFields.Create(AOwner: TComponent);
begin
  inherited Create(AOwner, TMyField);
end;

function TMyFields.GetItem(Index: Integer): TMyField;
begin
  result := (inherited GetItem(Index)) as TMyField;
end;

function TMyFields.Insert(Index: Integer): TMyField;
begin
  result := (inherited Insert(Index)) as TMyField;
end;

procedure TMyFields.SetItem(Index: Integer; const Value: TMyField);
begin
  inherited SetItem(Index, Value);
end;

procedure TMyFields.Update(Item: TmyField);
begin
  inherited Update(Item);
end;

procedure Register;
begin
  RegisterComponents('MyComponents', [TMainComponent2]);
end;

end.

您忘记包含继承的
TMyField
的名称路径:

function TMyField.GetNamePath: string;
begin
  Result := inherited GetNamePath + Format('MyField%d',[Index]);
end;
此外,您为
MyFields
属性指定了错误的所有者。使用
Self
而不是
AOwner
AOwner
是您放置组件的表单

constructor TMainComponent2.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FMyFields := TMyFields.Create(Self);
end;

B.t.w.为什么重新引入
TMyFields.Update
?这打破了继承链。尽可能使用
覆盖
,或添加其他方法。

谢谢!它起作用了。我使用reintroduce是因为当我使用重写编译器时,会说:“更新与以前的声明不同”。TOwnedCollection已更新(项:TCollectionItem)。我有更新(项目:TMyField)。您不必:
TMyField
是一个
t集合项目
。如果您需要将其视为<代码> TMyfield < /C> >,然后在 Update < /Cord>方法中将其转换为此。请考虑。