Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

assigning procedures to events at run time

953 views
Skip to first unread message

Neil White

unread,
Mar 19, 1999, 3:00:00 AM3/19/99
to
HOw do i do this?
this is what i have got but is keeps saying " Not enough actual parameters
"

button1.OnClick = Procedure buttondun(buttonnumber: integer);

NOTE: this is just an example
Could you help me with this?

Neil White Litt...@usa.net

Steve Schafer (TeamB)

unread,
Mar 19, 1999, 3:00:00 AM3/19/99
to
On Fri, 19 Mar 1999 17:12:15 +1300, "Neil White" <litt...@usa.net>
wrote:

>HOw do i do this?
>this is what i have got but is keeps saying " Not enough actual parameters
>"
>
>button1.OnClick = Procedure buttondun(buttonnumber: integer);
>
>NOTE: this is just an example
>Could you help me with this?

Rule #1: A method used as an event handler must be exactly that: a
method. It can't be a standalone procedure.

Rule #2: An event is declared to be of a particular type (for example,
TButton.OnClick is of type TNotifyEvent). The parameter signature of
any method you assign as an event handler must exactly match that of
the event. Thus, for an event of type TNotifyEvent, the method must
take exactly one parameter (Sender) of type TObject.

So, given the above, this is how you might assign an event handler at
run time:

1) Declare an appropriate event-handling method. It doesn't really
matter what object this method belongs to, although you would normally
add it to the form containing the component whose event you want to
handle. For example:

type
TForm1 = class(TForm)
...
public
procedure MyClickHandler(Sender: TObject); <-- add this
...
end;

2) Define the method by giving it a body in the implementation section
of the unit:

procedure TForm1.MyClickHandler(Sender: TObject);
begin
ShowMessage(Format('you clicked a %s', [Sender.ClassName]));
end;

3) At run time, assign the handler to an event:

Button1.OnClick := MyClickHandler;

Depending on where you have the above assignment, you may need to
qualify the references:

Form1.Button1.OnClick := TForm1.MyClickHandler;

-Steve


Neil White

unread,
Mar 19, 1999, 3:00:00 AM3/19/99
to

Thanx.
Is there a way of fining the name of the object rather then the name of the
class it is part of?

-Neil

Steve Schafer (TeamB)

unread,
Mar 19, 1999, 3:00:00 AM3/19/99
to
On Fri, 19 Mar 1999 22:49:50 +1300, "Neil White" <litt...@usa.net>
wrote:

>Is there a way of fining the name of the object rather then the name of the


>class it is part of?

Not all objects have a Name property--only TComponent and its
descendants do. So, you have to use a typecast:

ShowMessage((Sender as TComponent).Name);

This will fail with an exception if Sender is not a descendant of
TComponent. Another way to do it would be something like this:

if Sender is TComponent then
ShowMessage(TComponent(Sender).Name)
else
ShowMessage('Sender is not a TComponent');

-Steve


0 new messages