Board index » delphi » Create event handler at runtime

Create event handler at runtime

Hi,
This is what I want to do.
1. I declare an array of components.
        MyMemo : Array[1..50] of TMemo
2. Then I create the instance as I need them e.g. when button is
clicked.
        MyMemo[i] := TMemo.Create(Parent);

3. Now comes my problem. I want to specify an different onclick event
handler for every instance like
        MyMemo[i].Onclick := MyMemo[i]onclick

I do not know the value of 'i' before it is needed

How do I do this without creating 50 eventhandler at designtime?

Hope this makes sense.

Thanks

That`s very funny Scotty. Now beam down my clothes!

Remove dashes in E-Mail.

 

Re:Create event handler at runtime


Quote
In article <35b2cf56.5078...@196.4.160.8>, Ant...@-osi.co.za (Anton) writes:
>This is what I want to do.
>1. I declare an array of components.
>    MyMemo : Array[1..50] of TMemo
>2. Then I create the instance as I need them e.g. when button is
>clicked.
>    MyMemo[i] := TMemo.Create(Parent);

Not like this you don't <g> - the Create method for a TMemo has no parameter.
In any case the usual parameter for Create when an object does has one is the
Owner. The Owner is responsible for freeing the object, so having no owner
_you_ will have to free it (see below). The Parent (which you will have to
nominate with MyMemo[i].Parent := SomeComponent) is resposible for passing
Windows messages to the new object. Without a Parent the new object does not
display, nor do they perform windows controlled actions.

Quote
>3. Now comes my problem. I want to specify an different onclick event
>handler for every instance like
>    MyMemo[i].Onclick := MyMemo[i]onclick

>I do not know the value of 'i' before it is needed

>How do I do this without creating 50 eventhandler at designtime?

1 An event handler does not necessarily have a reference to the object name in
the event handler's name. Delphi generated event handlers (when you
double-click on the item or in the Object Inspector's event handler selection
box) uses the object's name as a convenience. But you can call it what you want
TForm1.MyVeryOwnSillyName(Sender : TObject); It is helpful to use the word
Click or DbleClk or some reference to what the _event_ is, in the name.

2 You allocate it to the object with MyObject.OnClick := MyVeryOwnSillyName;
and  an event handler can be allocated to as many object events as you like
_provided_ it is of the correct procedure type. This means that the parameters
must be those required. Most events have a procedure type of TNotifyEvent.
These are an object method and have one parameter of TObject. That TObject is
the object which generated the event.

3 You have many options to allocate the event handler :-
a) Have one event handler with lots of "if else" statements in it :-

if TMemo(Sender) is Memo[1] then
 . . . .
else
  if TMemo(Sender) is Memo[2] then
   . . .
  else
   if Tmemo(Sender) is Memo[3] then . . . etc etc

4 Have many event handlers and allocate the event handler in the Create
procedure :-

Memo[i] := TMemo.Create;
case i of
  1..5 : Memo[i].OnClick := ThisOne;
  6..8 : Memo[i].OnClick := ThatOne;
  9..15 : Memo[i].OnClick := TheOtherOne;
 . . . etc

If the event handler is allocated to more than one object, you will still have
to identify which object triggered the event handler as in 1 above.

But in all these options the event handler code must have been written at
design-time.

Don' t forget to Free all the TMemo's of the array in the form Close event (or
elsewhere) when you have finished with them. Freeing is made safer if you set
the TMemo array contents to nil when you enter the program :-

in FormActivate :-

for i := 0 to High(MyMemo) do
  MyMemo[i] := nil;

and in the Close (or other place) :-

for i := 0 to High(MyMemo) do begin
  MyMemo[i].Free;
  MyMemo[i] := nil;
end;

Have fun <g>

Alan Lloyd
alangll...@aol.com

Other Threads