Board index » delphi » how to assign a procedure to a event property

how to assign a procedure to a event property

Hi
  How can I assign my own procedure to OnErrorOccurred , thanks a lot.

type
  TErrorOccurred = procedure (Sender: TObject; Socket: Integer; Error:
integer; Msg: string) of object;

  TSockets = class(TWinControl)
  private
    FErrorOccurred: TErrorOccurred;

  published
    property OnErrorOccurred: TErrorOccurred read FErrorOccurred  write
FErrorOccurred;
  end;

 

Re:how to assign a procedure to a event property


In article <3364487A.6...@netvigator.com>,
   kevinlin <kevin...@netvigator.com> wrote:

Quote
>  How can I assign my own procedure to OnErrorOccurred , thanks a lot.
>type
>  TErrorOccurred = procedure (Sender: TObject; Socket: Integer; Error:
>integer; Msg: string) of object;
>  TSockets = class(TWinControl)
>  private
>    FErrorOccurred: TErrorOccurred;
>  published
>    property OnErrorOccurred: TErrorOccurred read FErrorOccurred  write
>FErrorOccurred;
>  end;

It's pretty easy, really, but it CAN'T be just a regular procedure -
it must be a method of a class (hence the "of object" in the type
definition)

If you've got a form, for example, that displays the error in the
caption and beeps:

type
  TForm1 = class(TForm)
  // etc.
  private
    procedure AnErrorOccurred(Sender: TObject; Socket: Integer;
      Error: Integer; Msg: String);
  end;

In your implementation:

procedure TForm1.AnErrorOccurred(Sender: TObject; Socket: Integer;
                                 Error: Integer; Msg: String);
begin
  MessageBeep(0);
  Caption := Msg+' in socket '+IntToStr(Socket);
end;

To assign it, then perhaps in the FormCreate, or after you've created
a socket, do this assignment:

  MySocketObject.OnErrorOccurred := AnErrorOccurred;

Or, if it's from another form/unit you're doing the assignment:

  MySocketObject.OnErrorOccurred := Form1.AnErrorOccurred;

Hope that helps :)

  --=- Ritchie Annand

Other Threads