Delphi 2009无法分配自定义组件的事件

3

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

我还为这个组件创建了一个自定义事件。唯一的问题是,虽然该事件已发布并出现在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.

3
我觉得没问题。我看不出你发布的代码有什么毛病。 - Ken White
3
代码没有问题;事件正在被触发(已经在 D2009 上进行了测试)。只有一个离题的注释 - 在这种情况下,您不需要为FOnBeforeGet编写setter,因此可以省略SetOnBeforeGet并直接使用property OnBeforeGet: TNotifyEvent read FOnBeforeGet write FOnBeforeGet; - TLama
1个回答

0

感谢大家的评论,也感谢TLama的提示。

事实证明我在表单上犯了一个错误。我从工具面板上拖放了自定义控件,并在Form.Create中创建了另一个同名的控件,我认为这导致了问题。现在已经修复了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接