Board index » delphi » Events for run-time create components

Events for run-time create components

An application I am working on needs to create some components at run-time.
The problem is how to inherit an existing component's event, or create at
run-time new events.

Here is my approach:

....
type
 { an array of RichEdit components }
 TRich = array [1..100] of TRichEdit;
....
var
  Rich: TRich;
  { a counter used to create new RichEdit components }
  { and 'i' is used to refer array element }
  Count, i: Integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
{ set counter to zero }
Count := 0;
end;

procedure TForm1.cmdNewRichClick(Sender: TObject);
begin
{ increase the count }
Count := Count + 1;
{ create a new RichEdit component in array }
Rich[Count] := TRichEdit.Create(self);
  with Rich[Count] do
  begin
  Parent := Form1;
  Text := 'RichEdit';
  end;
end;

end.

As far as creating and handling run-time components this seems to work
fine, but Rich[i] has no events, such as OnChange, or OnKeyDown. Is it
possible for these new RichEdit components, in the array, to respond to
events, or inherit events from an existing RichEdit component? If so How?

Anthony M Walter
email: antho...@metrolink.net

 

Re:Events for run-time create components


"Anthony M Walter" <antho...@metrolink.net> wrote:

Quote
>An application I am working on needs to create some components at run-time.
>The problem is how to inherit an existing component's event, or create at
>run-time new events.

[...]

Quote
>As far as creating and handling run-time components this seems to work
>fine, but Rich[i] has no events, such as OnChange, or OnKeyDown. Is it
>possible for these new RichEdit components, in the array, to respond to
>events, or inherit events from an existing RichEdit component? If so How?

The main thing to remember is that event handlers are very much like
properties, that is, you can assign a different event handler at
run-time, as you can set a property's value.
You can set Rich[Count].OnChange, as you can set Rich[Count].Parent or
Rich[Count].Text.
You have to write a procedure, say "procedure RichOnChange..." that
suits the OnChange or OnKeyDown event handlers procedure type and
assign it to the Rich[Count].OnChange, with:
Rich[Count].OnChange:= RichOnChange;

Hope this helps

Quote
>Anthony M Walter
>email: antho...@metrolink.net

Diogo Assuncao
d...@ip.pt
http://members.tripod.com/~DiogoAssuncao

ConTar - Electronica Industrial, Lda.
PORTUGAL
con...@mail.telepac.pt

Other Threads