Delphi 2009未分配自定义组件的事件

Delphi 2009未分配自定义组件的事件,delphi,events,event-handling,delphi-2009,custom-component,Delphi,Events,Event Handling,Delphi 2009,Custom Component,我创建了一个从THTTPReqResp继承的自定义组件TCustomHTTPReqResp 我也为这个组件创建了一个自定义事件。我唯一的问题是,尽管事件已发布并显示在IDE上,但当我分配事件处理程序并运行应用程序时,不会调用事件处理程序 但是,如果将其分配到表单上的代码上,请创建,即: CustomHTTPReqResp1.OnBeforeGet := CustomHTTPReqResp1BeforeGet; 它起作用了。除此之外,其他一切都很好 你做错事了吗?提前谢谢 以下是自定义组件的代码

我创建了一个从THTTPReqResp继承的自定义组件TCustomHTTPReqResp

我也为这个组件创建了一个自定义事件。我唯一的问题是,尽管事件已发布并显示在IDE上,但当我分配事件处理程序并运行应用程序时,不会调用事件处理程序

但是,如果将其分配到表单上的代码上,请创建,即:

CustomHTTPReqResp1.OnBeforeGet := CustomHTTPReqResp1BeforeGet;
它起作用了。除此之外,其他一切都很好

你做错事了吗?提前谢谢

以下是自定义组件的代码:

unit CCustomHTTPReqResp;

interface

uses
  SysUtils, Classes, Dialogs, SOAPHTTPTrans;

type
  TCustomHTTPReqResp = class(THTTPReqResp)
  private
    { Private declarations }
    FOnBeforeGet: TNotifyEvent;
    procedure DoOnBeforeGet;
  protected
    { Protected declarations }
    procedure SetOnBeforeGet(const AOnBeforeGet: TNotifyEvent);
  public
    { Public declarations }
    constructor Create(Owner: TComponent); override;
    destructor Destroy; override;
    procedure Get(Resp: TStream); override;
  published
    { Published declarations }

    { Events }
    property OnBeforeGet: TNotifyEvent read FOnBeforeGet write SetOnBeforeGet;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('My Components', [TCustomHTTPReqResp]);
end;

{ TCustomHTTPReqResp }

constructor TCustomHTTPReqResp.Create(Owner: TComponent);
begin
  inherited Create(Owner);
  // Code here.
end;

destructor TCustomHTTPReqResp.Destroy;
begin
  // Code here.
  inherited;
end;

procedure TCustomHTTPReqResp.SetOnBeforeGet(const AOnBeforeGet: TNotifyEvent);
begin
  FOnBeforeGet := AOnBeforeGet;
end;

procedure TCustomHTTPReqResp.DoOnBeforeGet;
begin
  if Assigned(FOnBeforeGet) then
  begin
    FOnBeforeGet(Self);
  end
  else
  begin
    MessageDlg('No Before Post Event Handler found!', mtInformation, mbOKCancel, 0);
  end;
end;

procedure TCustomHTTPReqResp.Get(Resp: TStream);
begin
  // Raise OnBeforeGet.
  DoOnBeforeGet;
  inherited Get(Resp);
end;


end.

谢谢大家的评论,谢谢特拉玛的提示


原来我在表格上搞错了。我从工具选项板中删除了表单上的自定义控件,并在表单上创建了另一个。使用相同的名称创建,我认为这导致了问题。现在修好了。

我觉得不错。我看不出你发布的代码有什么问题;正在激发事件(在D2009上测试确定)。只需一个主题外注释-在这种情况下,您不需要设置
FOnBeforeGet
,因此您可以保存
SetOnBeforeGet
,并直接使用
属性OnBeforeGet:TNotifyEvent read FOnBeforeGet write FOnBeforeGet