component nested in component



Hi,

I have the following problem:
I created my own button-component that I want to link with a popupmenu
which I want to create dynamically in my button-component.
When using the component in delphi the user should NOT be able to link
the button component with it's own popupmenu but should only be able to
edit the menu's items.

That's what I've done to achieve this:

private
fPopupMenu : TMainMenu;
published
property Menu: TMainMenu read fPopupMenu;

constructor TMyPopupButton.create(aOwner:TComponent);
begin
inherited create(aOwner);
fPopupMenu := TMainMenu.create(self);
end;

Somehow that doesn't work as planned. Being placed on the form for the
first time the component adds this to the DFM:

object TMyPopupButton: TLtPopupButton
[...]
object TMainMenu
end
end

But as soon as I do something like a cut'n'paste with my component an
additional mainmenu is created on the mainform(!).

object Form1: TForm1
object TMyPopupButton: TMyPopupButton
[...]
object TMainMenu
end
end
object TMainMenu
end
end

I already read a little bit about getchildren and getchildparent but I
think that was just interested for older versions of Delphi, right?
I am using Delphi 7.

I guess this is somehow connected to the fact that the constructor is
NOT only called when the component is placed on the form but also when
I do something like a cut'n'paste. Maybe I will have to check
componentstate in the contructor and only create the mainmenu when it's
in "creation"-mode?
Or maybe there's another function that's called only once when the
component is placed on the form?

Thanks in advance for your help.

Juergen

.


Quantcast