Link to home
Start Free TrialLog in
Avatar of qwertyuiopasdfghjkl
qwertyuiopasdfghjkl

asked on

TNotifyEvent ?

i'm beginner of delphi 4, i've bought the delphi books and online help, but i still dont understand how to use TNotifyEvent. can anyone here give me a simple example code.
ASKER CERTIFIED SOLUTION
Avatar of dwwang
dwwang

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
TNotifyEvent is simply a pointer to a function... Here is how a TNotifyEvent is beign declared...

type
  TSomeEvent = procedure(Sender : TObject);

This is an example of creating your own event...

all the OnCLick() OnMouseMove().. (the ones that are On..) are TNotifyEvents which are executed when that event happens... For example you click on the form and the OnClick() of the form is being executed...

—Viktor
——Ivanov
Avatar of qwertyuiopasdfghjkl
qwertyuiopasdfghjkl

ASKER

viktornet,
then how do i make it automatically fire the event ?



Hi, just simply write the event handler as I describe above, then at the property editor you can find the procedure by click the comboBox of one event.

If you want to assign the handler to an invisible component, such as TClientSocket, just write  like:
myClientSock.OnReceiveData:=MyEventHandler(....) //with proper parameter list.
You can't make it automatically fire the event... You should include that event to be fired somewhere. take this for example... Here i create a TNotifyEvent  property so when the component is painted the code in the OnPaint is executed...

//Component definition and stuff....
type
  TMyComp = class(TGraphicControl)
{...}
private
  FOnPaint : TNotifyEvent;
  procedure Paint; override;
{...}
published
  property OnPaint : TNotifyEvent read FOnPaint write FOnPaint;
{...}
procedure TMyComp.Paint;
begin
  //Maybe some code here...
  if Assigned(FOnPaint) then FOnPaint(self);
  //And probably some more in here...
end;

Now when the component compiled and everything you can drop it on your form and then assign some code in the OnPain() as you do in other controls...

procedure TForm1.MyComp1Paint(Sender : TObject);
begin
  ShowMessage('Our new component is being painted at the moment');
end;

When your component is repainted the message above will be shown...

This is a way to use TNotifyEvent... Usually you make your application take the message like WM_PAINT and then create and event for that,,,

Hope this helps a bit.

Cheers,
Viktor
oh, yeah... as Wang mentioned you can do stuff like this...

type
  TForm1 = class(TForm)
{whatever}
public
  procedure MyOnClickEvent(Sender : TObject);
{stuff}
end;

procedure TForm1.MyOnClickEvent(Sender : TObject);
begin
  ShowMessage('There you go. We just experienced the OnClick() of ' + TObject(Sender).Name);
end;

Now anywhere in your program you can do stuff like this...

NameOfMyControl.OnClick := MyOnClickEvent;
e.g.
Form1.OnClick := MyOnClickEvent;

Hope this helps...

-Viktor
--Ivanov