Board index » delphi » How to Assign event handlers to objects(components) created at run time

How to Assign event handlers to objects(components) created at run time

I am creating objects like buttons etc at run time in my form, i also
want to assign code to the event click of the button, created at run
time. so that when the user clicks the button my code should run. how do
i do it ?

Procedure Tform1.Formcreate;
begin

btn := Tbutton.create(form1);

{ how do i attach my code to the button click of btn }

end;

Procedure Mycodeforbtnclick;
begin
  { my code to run on btn click }
end;

-------------------==== Posted via Deja News ====-----------------------
      http://www.dejanews.com/     Search, Read, Post to Usenet

 

Re:How to Assign event handlers to objects(components) created at run time


sjs74n...@hotmail.com schrieb in Nachricht
<886506680.1130692...@dejanews.com>...

Quote
>Procedure Tform1.Formcreate;
>begin

>btn := Tbutton.create(form1);

>{ how do i attach my code to the button click of btn }

>end;

>Procedure Mycodeforbtnclick;
>begin
>  { my code to run on btn click }
>end;

>-------------------==== Posted via Deja News ====-----------------------
>      http://www.dejanews.com/     Search, Read, Post to Usenet

Versuch es mal mit btn.OnClick:=MyCodeForBtnClick;

Viel Spa? noch
Sven Opitz
S.Op...@Cardy.de

Re:How to Assign event handlers to objects(components) created at run time


On Tue, 3 Feb 1998 14:06:28 -0000,
"Sven Opitz" <S.Op...@Cardy.de> said:

Quote
> sjs74n...@hotmail.com schrieb:
> >Procedure Tform1.Formcreate;
> >begin
> >btn := Tbutton.create(form1);
> >{ how do i attach my code to the button click of btn }
> >end;
> >Procedure Mycodeforbtnclick;
> >begin
> >  { my code to run on btn click }
> >end;
> Versuch es mal mit btn.OnClick:=MyCodeForBtnClick;

Achte nur darauf dass "MyCodeForBtnClick" vom richtigen Typ ist:
Just make sure that "MyCodeForBtnClick" is of the correct type:

Procedure MyCodeForBtnClick(Sender: TObject);
begin
  { Your code to run on btn click }
end;

Am einfachsten schoepft man diese Prozedure dadurch dass man sie erst im
Object Inspector schoepfen laesst durch doppelklicken im Feld "OnClick".
Danach nur den Namen von diesem Feld wegradieren, so viele zusaetzliche
machen wie noetig, dann im programm den jeweils richtigen laut Svens
Rezept anknuepfen.

The easiest way to create these procedures is to let the Object
inspector build them for you, by double-clicking on the "OnClick" event.
Then you can delete the procedure name from that edit field, make as may
more procedures as you need in the same way, and then connect them in
code like Sven says.

        Christian R. Conrad
--
Proud and sole owner of all opinions (except quotes) expressed above!
======================================================================
     "Wave goodbye to RISC. It's the rotary engine of computing."
            Brad Aisa, in comp.lang.pascal.delphi.advocacy

Re:How to Assign event handlers to objects(components) created at run time


Quote
In article <886506680.1130692...@dejanews.com>, sjs74n...@hotmail.com wrote:

]-I am creating objects like buttons etc at run time in my form, i also
]-want to assign code to the event click of the button, created at run
]-time. so that when the user clicks the button my code should run. how do
]-i do it ?
]-
]-
]-Procedure Tform1.Formcreate;
]-begin
]-
]-btn := Tbutton.create(form1);
]-
]-{ how do i attach my code to the button click of btn }
]-
]-
]-end;
]-
]-Procedure Mycodeforbtnclick;
]-begin
]-  { my code to run on btn click }
]-end;

make Mycodeforbtnclick a method of some object...it's probably
easiest to make it a method of your form...

  Procedure Tform1.Mycodeforbtnclick;

then
  btn := Tbutton.create(form1);
  btn.OnClick := form1.Mycodeforbtnclick;

for more info, search the on-line help for "method pointers"

Mark Vaughan

Re:How to Assign event handlers to objects(components) created at run time


Quote
sjs74n...@hotmail.com wrote:

> I am creating objects like buttons etc at run time in my form, i also
> want to assign code to the event click of the button, created at run
> time. so that when the user clicks the button my code should run. how do
> i do it ?

> Procedure Tform1.Formcreate;
> begin

> btn := Tbutton.create(form1);

> { how do i attach my code to the button click of btn }

> end;

> Procedure Mycodeforbtnclick;
> begin
>   { my code to run on btn click }
> end;

At first OnClick event handler have to be an object method and:

 Procedure TMyType.Mycodeforbtnclick(Sender : TObject);
 begin
   { my code to run on btn click }
 end;

or

 Procedure TForm1.Mycodeforbtnclick(Sender : TObject);
 begin
   { my code to run on btn click }
 end;

Procedure Tform1.Formcreate;
begin

 btn := Tbutton.create(form1);

 btn.OnClick := FMyObject.Mycodeforbtnclick;
{
  or if you attached this handler to TForm1 class:
 btn.OnClick := Mycodeforbtnclick;

Quote
}

 end;

Best regards,
        Eugene
--
#-----------------------------------------
# Name: Eugene V. Mishura            
# Location: Kiev, Ukraine
#-----------------------------------------
# Phone:        (+380 44) 4426077 (office)
#               (+380 44) 4437925 (office)
# E-Mail:       mish...@ksf.kiev.ua
#
#               NO SPAM, PLEASE
#
#-----------------------------------------

Re:How to Assign event handlers to objects(components) created at run time


sjs74n...@hotmail.com wrote in article
<886506680.1130692...@dejanews.com>...

Quote
> I am creating objects like buttons etc at run time in my form, i also
> want to assign code to the event click of the button, created at run
> time. so that when the user clicks the button my code should run. how do
> i do it ?

> Procedure Tform1.Formcreate;
> begin
> btn := Tbutton.create(form1);
> end;

> Procedure Mycodeforbtnclick;
> begin
>   { my code to run on btn click }
> end;

You should add then something like this:

btn.OnClick:=Mycodeforbtnclick;

( you know that events are in fact properties of the components you're
using, and
their names start with On - it's only a convention, but it is followed by
most component developers. You should keep in mind that your handler should
have the same type ( formal parameters) as the usual handler )

 I hope this helped you!

Re:How to Assign event handlers to objects(components) created at run time


On 13 Feb 1998 14:23:57 GMT,
"Dragos Iulian" <d...@main.litm.sorostm.ro> said:

Quote
> sjs74n...@hotmail.com wrote in article
> > Procedure Tform1.Formcreate;
> > begin
> > btn := Tbutton.create(form1);
> > end;
> > Procedure Mycodeforbtnclick;
> > begin
> >   { my code to run on btn click }
> > end;
> You should add then something like this:
> btn.OnClick:=Mycodeforbtnclick;

You forgot to point out that this won't work with
his procedure -- it's of the wrong type, isn't it?

[MUNCH events are properties]

Quote
> You should keep in mind that your handler should have
> the same type ( formal parameters) as the usual handler )

The correct procedure has a declaration like:
Procedure Mycodeforbtnclick(Sender: TObject);

Quote
>  I hope this helped you!

So do I, but I find I must agree with those who complain (in another
thread, on c.l.p.d.a.) about the stupidity of the questions people ask:

Haven't these people ever generated a button-click event-handler in the
Delphi IDE, by double-clicking either the button on the form, or in the
empty field for the event in the Object Inspector? Nobody ever noticed
the declaration of those procedures, with a (Sender: TObject) parameter,
in the code editor? Is there, any simpler way to create your first "own"
event handler than letting Delphi make one for you, and then copy and
paste it (and its interface declaration) in the code editor, and change
the name? Yes, actually, there is: Let Delphi build one, rename it in
the Object Inspector -- this renames both the implementation _and_ the
interface section declarations -- delete its name from the event-handler
field of the Object Inspector, then double-click there _again_! Repeat
ThisWay until (NumberOfGeneratedProcedures = NumberOfWantedProcedures);

        Christian R. Conrad
--
Proud and sole owner of all opinions (except quotes) expressed above!
======================================================================
     "Wave goodbye to RISC. It's the rotary engine of computing."
            Brad Aisa, in comp.lang.pascal.delphi.advocacy

Other Threads