Board index » delphi » assigned? event?

assigned? event?

I have a component that I like to use with different applications (It's a comm
component, but this question has nothing to do with comm routines).  Each time
I write a new application, I have to re-write the routines (actually I copy &
paste them).  What I'm trying to do is create a seperate unit that has the
component in it, plus the routines that I commonly use.  This way, all I have
to do is include this seperate unit in whatever project I'm working on, and
I'll have the component with my favorite routines.  I've tried this by creating
an instance of the component like this:

unit   seperateunit (*unit containing component and favorite routines*)

interface
uses   windows, messages, sysutils, classes, graphics,
       controls, forms, dialogs, stdctrls, extctrls, favorite component;
(*here's the component*)

procedure... (* procedures to be accessed from main program*)
procedure...

implementation

var
Fcomponent: Tfavorite component (*instance of my component*)
(*procedures are declared from here below*)
(*among these is a procedure tied to an event that belongs to the component,
shown below*)

procedure FcomponentEvent (parameter, parameter, parameter);
begin
(*stuff to do*)
end;

Now, after all the procedures have been defined, I have an initialization call:

initialization
fcomponent:=Tfavoritecomponent.Create(fcomponent);
end.

I can't seem to get the event to trigger, because it won't do what's defined in
the event procedure.
How do I make sure that the event is noticed and responded to?  I know it has
something to do with the "assigned" function, but I don't know how to make it
return true.
Thanks in advance
-Butch

 

Re:assigned? event?


Hi,

I'm not sure if I understand you correctly, but here goes:

You should write code like this:

unit Unit1;

interface

type
  MyCommComponent = class(TComponent)
  private
// blah blah blah
    FMyEvent: TNotifyEvent;
  protected
// blah blah blah
    procedure MyEvent; virtual;
  public
// blah blah blah
published
// blah blah blah
    property OnMyEvent: TNotifyEvent read FMyEvent write FMyEvent;
  end;

implementation

procedure TMyCommComponent.MyEvent;
begin
  if Assigned (FMyEvent) then
    FMyEvent(Self);
end;

end;

Firstly, I am using TNotifyEvent because you weren't that specific. Change
to whatever you want.
Also, I have made the MyEvent procedure virtual, because it makes the
component more extensible, giving descendants the freedom to override it.

This code alone is not enough though. You must obviously call the event
handler somewhere - a message handler being the perfect place. Since I don't
know the comms api messages, I have added a message handler for wm_KeyDown
(I know, silly example, but it demonstrates the idea:-)

Add to the protected section:

procedure WMKeyDown(var Message: TMessage); message wm_KeyDown;

And add to your implementation:

procedure TMyCommComponent.WMKeyDown;
begin
// blah blah blah

  MyEvent;

// blah blah blah
end;

Remember that you cannot assign an event handler to the event from within
the component. Do your processing in the message handler, and only call the
event handler where it is safe to call the user's code.

I hope this helps.

Kindest Regards,
Cobus Kruger

Other Threads