Delphi 中的事件

类似C# 的委托与事件

//zhang

//中间类
unit ClientSys;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, Menus,
  StdCtrls, Dialogs, Buttons, Messages, ExtCtrls, ComCtrls, StdActns,
  ActnList, ToolWin, ImgList, DB, DBClient, ADODB, DBActns, AppEvnts ,typinfo
  ;
type
  TClientEventHandle = procedure (Content: string) of object;
  TGeneralSys = Class(TComponent)
  private
    FOnSearch :  TClientEventHandle ;
  public
    property  OnSearch: TClientEventHandle read FOnSearch write FOnSearch;
    procedure Search(Content: string);
  end;

var
  GeneralSys : TGeneralSys ;

implementation

procedure TGeneralSys.Search(Content: string);
begin
  if Assigned(FOnSearch) then FOnSearch(Content);  //触发事件

end;


end.

//调用
procedure TfrmMainForm.btnSearchClick(Sender: TObject);
begin
  GeneralSys.Search(self.ActiveMDIChild.name);
end;

//响应

private
    { Private declarations }
    procedure Search(Content : string);
   
   
procedure TfrmSupplierType.FormActivate(Sender: TObject);
begin
  GeneralSys.OnSearch := Search ;
end;

procedure TfrmSupplierType.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  GeneralSys.OnSearch := nil ;
  Action := caFree;

end;

posted @ 2011-05-27 10:19  abenz  阅读(238)  评论(0编辑  收藏  举报