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

发布于 2025-01-05 07:15:45 字数 1878 浏览 0 评论 0原文

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

I created a custom component TCustomHTTPReqResp inheriting from THTTPReqResp.

I did also create a custom event for this component. The only problem I'm having is that although the event is published and appears on the IDE, when I assign an event handler and run the application the event handler doesn't get called.

However if assign it on the code on Form.Create i.e.:

CustomHTTPReqResp1.OnBeforeGet := CustomHTTPReqResp1BeforeGet;

it works. Apart from this everything else works just fine.

Have a done something wrong? Thanks in advance.

Here is the code for the custom component:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日久见人心 2025-01-12 07:15:45

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

事实证明我在表格上犯了一个错误。我从工具选项板上删除了表单上的自定义控件,并在 Form.Create 上创建了另一个具有相同名称的控件,我认为这导致了问题。现在修好了。

Thanks for the comments everyone, and thanks TLama for the tip.

It turns out I made a mistake on the form. I dropped the custom control on the form from the Tool Palette and also created another one on Form.Create with the same name and I think this caused the problem. Fixed now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文