Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can I put the same TMenuItem in both TMainMenu and TPopupMenu?

622 views
Skip to first unread message

Stephen Smith

unread,
Jan 4, 1998, 3:00:00 AM1/4/98
to

This sure seems like a smart way to duplicate some often-used menu
items, but I can't seem to do it.

I would actually like to copy two submenus on my TMainMenu in their
entirety into my TPopupMenu at runtime. These two submenus have Tags
= 1. But the following code produces a "Menu Inserted Twice" runtime
error:

for i := 0 to MainMenu.Items.Count - 1 do begin
if (MainMenu.Items[i].Tag = 1) then begin
MainPopupMenu.Items.Insert(0,MainMenu.Items[i]);
end;
end;

Is there anyway to accomplish this?

Thanks,
Steve

Steve Zimmelman

unread,
Jan 4, 1998, 3:00:00 AM1/4/98
to

I believe you were getting the "Menu Inserted Twice" error because you were
using the pointer to an already created menu item, instead of creating a new
item.

There are two loops here. The first (outer) is for the menu Titles across
the top, the second (inner) is for the SubItems under the MenuTitle. I
don't think you can add an Item straight across, but you can assign a new
item it's properties.

The only thing I'm a little unclear about is the use of the Create for the
MenuItem and Freeing it. The examples I've seen in the PAS files that were
distributed with Delphi show MenuItems being Created and never freed. I
checked my system resources after running this several times, and noticed no
apparent leakage.

Var i,ii : Integer ;
MySubItem : TMenuItem;
Begin
For i := 0 To (MainMenu1.Items.Count-1) Do Begin
// MainMenu Title Loop
If MainMenu1.Items[i].Tag = 1 Then Begin
For ii:=0 To (MainMenu1.Items.Items[i].Count-1) Do Begin
// Menu SubItems Loop
If MainMenu1.Items[i].Items[ii].Tag = 1 Then Begin
MySubItem := TMenuItem.Create(Self);
PopupMenu1.Items.Add(MySubItem);
MySubItem.Caption := MainMenu1.Items[i].Items[ii].Caption ;
MySubItem.OnClick := MainMenu1.Items[i].Items[ii].OnClick ;
End;
End;
End;
End; //For
End;

-Steve-

Stephen Smith wrote in message <34b0822b...@news.mindspring.com>...

Jordan Russell

unread,
Jan 4, 1998, 3:00:00 AM1/4/98
to

Dynamically created menu items are automatically freed by the form when the
form is destroyed (but you can free them manually if necessary by calling
their Free methods). This is because your TMenuItem was created with "Self"
as the Owner:

MySubItem := TMenuItem.Create(Self);

In fact, any type of component created that way is freed in the same manner.
But if you specify "nil" for the owner, as in:

MySubItem := TMenuItem.Create(nil);

then you *would* have to manually free the item.

--
Jordan Russell
(Remove "xyz." from my address when replying to me by e-mail)

Steve Zimmelman wrote in message <68opm0$1t...@forums.borland.com>...

0 new messages