Board index » delphi » Creating dynamic objects

Creating dynamic objects

Hi everyone,

Ok, I've got some objects being created at runtime - a handful
of menu options, depending on how many objects the user has
created.  I've got this working fine, they get added and deleted
and the caption is right etc, etc... the only thing I can't
figure out is how to set methods for them.... ie, the OnClick()
event (a relatively useful one for menuitems :)

I tried:

newItem.OnClick:=dothis(i);

where do this is a procedure declared as:

procedure TForm1.dothis(i:integer);
....

but I get the error:

Incompatible types: 'TNotifyEvent' and 'procedure, untyped
pointer or untyped parameter'

(how, btw, do you copy an error message like that from the IDE?  
I had to write that by hand...).

I think I'm close - I just need to get the syntax of that line
right...

Regards

OgO
--
Unless otherwise specified, all my posts relate to Delphi 3.

My Freeware: http://qjc.cjb.net/freeware.html
My Win9x Cursors: http://qjc.cjb.net/cursors.html
Feel free to ICQ me.  My ICQ #: 12889482
I left my Glasses in my email - you better take them out!

 

Re:Creating dynamic objects


Quote
"OgO" <quent...@qjc.cjbGlasses.net> wrote in message

news:Xns916060C6F9287ogo1mynxwowaustcom@203.109.250.24...

Quote
> Hi everyone,

> Ok, I've got some objects being created at runtime - a handful
> of menu options, depending on how many objects the user has
> created.  I've got this working fine, they get added and deleted
> and the caption is right etc, etc... the only thing I can't
> figure out is how to set methods for them.... ie, the OnClick()
> event (a relatively useful one for menuitems :)

Type
    tForm1 = class (tForm)
        . . .
        private
            procedure HandleMenuClicks (Sender : tObject);
        . . .
        end;

someMenuItem.OnClick := HandleMenuClicks;

procedure tForm1.HandleMenuClicks (Sender : tObject);

begin
case (Sender as tMenuItem).MenuIndex of
    0 : // its the first item on the menu
    . . .
    N : // its the n'th item on the menu
    end;
end;

If you don't want to use MenuIndex you can compare against the Caption
property or assign a value to Tag and test that.

Re:Creating dynamic objects


In comp.lang.pascal.delphi.misc, on 21 Nov 2001, Bruce Roberts
announced:

Quote
> procedure tForm1.HandleMenuClicks (Sender : tObject);

> begin
> case (Sender as tMenuItem).MenuIndex of
>     0 : // its the first item on the menu
>     . . .
>     N : // its the n'th item on the menu
>     end;
> end;

> If you don't want to use MenuIndex you can compare against
> the Caption property or assign a value to Tag and test that.

Thanks Bruce, that works well.

I've done it

procedure tForm1.HandleMenuClicks (Sender : tObject);
var
   userNum:integer;
begin
     userNum := (Sender as tMenuItem).tag;
...
then work with usernum....

I suppose it depends on the context, but surely you would be
better almost every time doing it the second way?  The whole
point of creating dynamic objects is that you can have as many
of them as you want / need, therefore doing it the first way,
it's quite possible that you could create more objects than you
have cases for, and the second way is much neater and easier to
read (and less typing :)

Hmm, actually just reading it again - I've used the tag property
to simply specify an integer - which equals the menuindex!  Duh!

Don't mind me, I'll just sit here being brain-dead :)

Regards

OgO
--
Unless otherwise specified, all my posts relate to Delphi 3.

My Freeware: http://qjc.cjb.net/freeware.html
My Win9x Cursors: http://qjc.cjb.net/cursors.html
Feel free to ICQ me.  My ICQ #: 12889482
I left my Glasses in my email - you better take them out!

Other Threads