2012-02-13 58 views
3

我創建了自定義組件TCustomHTTPReqResp繼承自THTTPReqRespDelphi 2009未分配自定義組件事件

我也爲此組件創建了一個自定義事件。我遇到的唯一問題是,雖然事件已發佈並出現在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

對我來說很好。我看不出您發佈的代碼有任何問題。 – 2012-02-13 19:16:05

+3

代碼沒有問題;該事件正在被解僱(在D2009上進行測試)。在這種情況下,你不需要爲'FOnBeforeGet'設置一個setter,所以你可以保存SetOnBeforeGet並直接使用屬性OnBeforeGet:TNotifyEvent read FOnBeforeGet write FOnBeforeGet; – TLama 2012-02-13 20:24:22

回答

0

感謝您的意見大家,感謝TLama。

原來我在表單上犯了一個錯誤。我從Tool Palette的表單中刪除了自定義控件,並在Form.Create上創建了另一個具有相同名稱的控件,我認爲這導致了問題。現在修復。