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

标签 delphi events event-handling delphi-2009 custom-component

我创建了一个继承自 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/

相关文章:

delphi - 自定义组件和选项卡顺序

delphi - 为什么我的图标在带有 TTrayIcon 的托盘栏上很难看?

Java/Spring : How to specify order for nested transactions with TransactionSynchronizationManager

c# - 是否有通用的 InkCanvas StrokesChanged 类型的事件?

java - 为什么 EventListenerList 没有被替换? (或 : what are the pitfalls in replacing it? )

delphi - 提高写入文件的速度

delphi - 如何在对象检查器中查看属性的类型?

ios - Swift 尝试分派(dispatch)和订阅事件时出现问题

javascript - jQuery 的 click() 函数怎么会比 addEventListener() 快这么多?

javascript - 即使我可以监视其他方法,也无法监视事件处理程序