Board index » delphi » Assigning of event handlers to another form/unit

Assigning of event handlers to another form/unit

Greetings,

  Maybe someone knows whether a possibility exists to assign (in design-time,
via Object Inspector) an event handler from another form to event of
a component belonging to the current form. For example, if I have a query
component (Query1: TQuery) on Form1 and a another query (Query2: TQuery) on
datamodule DataMod2 and a handler inside Unit2 for event Query2.BeforeOpen,
can I assign Query1.BeforeOpen to the same handler via Object Inspector?
I tried this (using Delphi 3) but failed -- when I select Query1 and click
on BeforeOpen event, Object Inspector does not see suitable event handler
on the neighbour datamodule. Maybe I missed something or Property
Editor for events indeed cannot see into another form/datamodule in the same
project?

Alexander Kopilovitch                      a...@vib.usr.pu.ru
Saint-Petersburg
Russia

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum

 

Re:Assigning of event handlers to another form/unit


Yes, it is.

The object inspector only sees event handlers defined in the object as event handlers. You aren't, however, restricted to what the
object inspector would like to use. Type the following into the object inspector of Query1 in the BeforeOpen box:
Unit2.Query2.BeforeOpen.

Cheers
Pe

Re:Assigning of event handlers to another form/unit


Alexander,

The following worked for me, placed in the data module:

Charles Johnson

implementation

uses
  Unit1;

{$R *.DFM}

procedure TDataModule2.DataModule2Create(Sender: TObject);
begin
  Query1.BeforeOpen := Form1.Query1.BeforeOpen
end;

Re:Assigning of event handlers to another form/unit


Quote
Peter Spears wrote:

> Yes, it is.

> The object inspector only sees event handlers defined in the object as event handlers. You aren't, however, restricted to what the
> object inspector would like to use. Type the following into the object inspector of Query1 in the BeforeOpen box:
> Unit2.Query2.BeforeOpen.

Let me see if I can elaborate a little on what Peter is driving at.

In Delphi, an event-handler is ultimately identified by a
procedure-pointer within the object; such as the variable "FOnClick."
This pointer either points to the procedure that is to be called when
the "OnClick" event takes place, or it is nil in which case no call
occurs.

When you create event-handlers in the Object Inspector, Delphi makes the
process as simple as a double-click...  presto, it sets up the object to
point to your new procedure and puts you in the editor with the
framework of the necessary procedure already in place.  But (Unlike
Visual Basic!) that is NOT all that you can do!

First of all, you may not know that you can have several different
objects pointing to the same event-handling procedure.  If you have
fif{*word*249} buttons that all work the same way, they can all point to the
same procedure.  The "Sender" parameter allows you to determine who's
calling.

Second, as Peter said, you can get that pointer-value into that
pointer-variable anytime and any way you like; just be absolutely
certain that the reference you make will, in fact, be "good" at
runtime.  I've observed that a really good time to make some assignments
is, not at FormCreate, but the *first* time "OnActivate" occurs.  I use
a simple logical flag to ignore subsequent activate-calls (or sometimes,
set the OnActivate pointer to NIL within the event-handling routine!).

Last, the "Component Writer's Guide," thin and unobtrusive as it is, is
well worth reading, as are the comments in FORMS.PAS.  These explain in
some detail how the property-storing and assignment process actually
works at runtime when a form is being created.

Other Threads