Board index » delphi » Events for run time created components

Events for run time created 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 created components


Tony,

If I understand correctly, you have assigned an event to the first
RichEdit, or want to at runtime.  All you have to do to assign all of
the RichEdits (or any other component array) an event (yes, they already
exist) is to point to the procedure that you want to run in the event.
It is not possible to create a procedure at runtime.  So define your
events on the first RichEdit and then just redirect the newly created
ones to point to the event of the first one.  Would look something like
this:

In your create (OR on your button click, as you have it):

procedure TForm1.cmdNewRichClick(Sender: TObject);
begin
  Count := Count + 1;
  Rich[Count] := TRichEdit.Create(self);
  with Rich[Count] do
    begin
    Parent := Form1;
    Text := 'RichEdit';
    OnChange := The OnChange that you want.  Note that this must be of  
the correct type e.g. TNotifyEvent, TDataChange, etc.
    OnKeyPress := Rich1OnKeyPress(nil, nil, whatever);
  end;
end;

I often have to reassign events at runtime, and this is the way to do
it.  Hope this helps out!
Jason 'Wedge' Perry.

Quote
Anthony M Walter wrote:

> 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:

> 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?

Other Threads