Board index » delphi » Event-handling and runtime-created components

Event-handling and runtime-created components

Creating an array of SpeedButtons during runtime is easy and also to
assign properties to them in a loop, but what about the events ?
For example if I wanted all the buttons to use the same procedure on the

OnClick-event?
Im kinda stuck...
Any suggestions would be highly appreciated!
J. Erixon
erix...@algonet.se

 

Re:Event-handling and runtime-created components


Hi!

Simply Write the procedure in the private part of your Form:

TForm1 = class(TForm)
........
private
   // the parameters much match the event's parameters..
   procedure SpeedButtonClick(sender: TObject);
......
end;

Then, implement the procedure, and when you create your speedbutton, assign
it's onclick event to the procedure :

procedure TForm1.CreateButton;
var
   sp := TSpeedButton;
begin
    sp := TSpeedButton.Create(self);
    sp.parent := self;
    sp.Onclick := SpeedButtonClick; // assign the procedure
    .......
end;

That can be done with any event, provided the procedure you manually
declare and use has the same parameter list as the event.

Hope it helps!

..............
bedarch%@%videotron.ca
(remove % to get my real email address)

Jne <erix...@algonet.se> wrote in article <354E539E.84EEA...@algonet.se>...

Quote
> Creating an array of SpeedButtons during runtime is easy and also to
> assign properties to them in a loop, but what about the events ?
> For example if I wanted all the buttons to use the same procedure on the

> OnClick-event?
> Im kinda stuck...
> Any suggestions would be highly appreciated!
> J. Erixon
> erix...@algonet.se

Re:Event-handling and runtime-created components


In article <354E539E.84EEA...@algonet.se>, erix...@algonet.se says...

Quote

>Creating an array of SpeedButtons during runtime is easy and also to
>assign properties to them in a loop, but what about the events ?
>For example if I wanted all the buttons to use the same procedure on the
>OnClick-event?
>Im kinda stuck...
>Any suggestions would be highly appreciated!
>J. Erixon
>erix...@algonet.se

You can assign the event handlers as follows:

  SpdBtnArray[i].OnClick := MyClickProc;

where MyClickProc is your procedure that handles the event. Remember that
MyClickProc must have the same header structure as the onclick (or whatever)
event eg. for OnClick it must be defined as

procedure TForm1.MyClickProc(Sender : TObject);
begin
.......
end;

--
Christo Crause
Thermal Separations Research
University of Stellenbosch
South Africa

Re:Event-handling and runtime-created components


MyArray[i].OnClick:=SpeedButtonsClick;

assuming MyArray: array [x..y] of TSpeedButton;

Quote
Jne <erix...@algonet.se> wrote:

: Creating an array of SpeedButtons during runtime is easy and also to
: assign properties to them in a loop, but what about the events ?
: For example if I wanted all the buttons to use the same procedure on the

: OnClick-event?
: Im kinda stuck...
: Any suggestions would be highly appreciated!
: J. Erixon
: erix...@algonet.se

Re:Event-handling and runtime-created components


In article <6jbgqi$62...@turtle.stack.nl>

Quote
Roel Spapens <ro...@stack.nl> writes:
> MyArray[i].OnClick:=SpeedButtonsClick;

> assuming MyArray: array [x..y] of TSpeedButton;

> Jne <erix...@algonet.se> wrote:
> : Creating an array of SpeedButtons during runtime is easy and also to
> : assign properties to them in a loop, but what about the events ?
> : For example if I wanted all the buttons to use the same procedure on the

> : OnClick-event?
> : Im kinda stuck...
> : Any suggestions would be highly appreciated!
> : J. Erixon
> : erix...@algonet.se

Absolutely. I've even used TLists of objects. Assigning event handlers
requires a little bit of forethought (e.g. what is it that my
component(s) will want to do -- I'll need to handle these cases and
possibly more). You get the idea. Click is the most natural of events
to want to use of course. I've even used the OnKeyPress event for other
processing.

===========================
Mike Powell
mi...@rt66.com
http://www.rt66.com/~mikep/

Other Threads