我创建了一个继承自 THTTPReqResp 的自定义组件 TCustomHTTPReqResp。
我还为此组件创建了一个自定义事件。我遇到的唯一问题是,虽然事件已发布并出现在 IDE 上,但当我分配事件处理程序并运行应用程序时,事件处理程序不会被调用。
但是,如果将其分配在 Form.Create 的代码上,即:
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.
最佳答案
感谢大家的评论,也感谢TLama的提示。
事实证明我在表格上犯了一个错误。我从工具选项板上删除了表单上的自定义控件,并在 Form.Create 上创建了另一个具有相同名称的控件,我认为这导致了问题。现已修复。
关于Delphi 2009 不分配自定义组件的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9265763/