Board index » cppbuilder » Dynamically creating/destroying TMenuItem

Dynamically creating/destroying TMenuItem

Quote
"Michael Lockey" <mloc...@hartlepool.com> wrote in message

news:38EEC297.54A643C5@hartlepool.com...

Quote
> When I create them, how do I assign the OnClick event to some
> code?

TMenuItem *menuitem = new TMenuItem(this);
menuitem->OnClick = MyClickHandler;

void __fastcall TForm1::MyClickHandler(TObject *Sender)
{
    // your click code here

Quote
}
> However, if this TMenu has sub-menus underneath, I can't figure how to
> get at them- not in any efficient manner, at least.

If you delete a menu, all of it's submenus get deleted for you as well.

Gambit

 

Re:Dynamically creating/destroying TMenuItem


...And I apologize; it's been covered before, but I didn't need to save
then...

I must dynamically create and delete menu items.  When I create them,
how do I assign the OnClick event to some code?

I was quite surprised that neither Delete() nor Remove() frees the
memory (since if I have to redelare the menu item later, I'm warned it
already exists). The following Code works for first level entries:
delete MenuBatchReport->Items[i - 1];

However, if this TMenu has sub-menus underneath, I can't figure how to
get at them- not in
any efficient manner, at least.

Many thanks.
--
Michael Lockey
director, Hartlepool Systems International
note: $10.00 fee (US) for all unsolicited spam mail.  This includes all
mail that is not either specifically requested, or a personal
communication.  By sending me anything that may qualify as spam, you
have agreed to the terms and conditions of my service.  If in any doubt,
you may send me an enquiry as to whether or not your message will be
construed as Spam.  There is no charge for this service.

Re:Dynamically creating/destroying TMenuItem


Quote
Remy Lebeau wrote:

> If you delete a menu, all of it's submenus get deleted for you as well.

Many thanks; at least I know I'm not crazy.

But it doesn't work.  (My code is irrelevant, but what I'm trying to end
up with is a variable number of menus and submenus, at the end of which
the operator will either view or print a report.  So, stripped to the
essentials, I do the following:

    j = MnBatchRpt->Count - 1;
    for (i = j; i >= 0; i--)
        {
        AnsiString T = MnBatchRpt->Items[i]->Name;
        MnBatchRpt->Remove(MnBatchRpt->Items[i]);
        }

With the simplest code, I'll just have two TMenuItems: MnBatchRptView
and MnBatchRptPrint (that's what the AnsiString is for; just to check.
So I remove the two little {*word*81}s (I've also tried Delete) and continue
on to recreate them

    TMenuItem *NewPItem = new TMenuItem(Name);
    NewPItem->Caption = "Print";
    NewPItem->Name = Name->Name + NewPItem->Caption;
    NewPItem->OnClick = ShowMUR;

(Name may have been something different, but in this case it's TMenuItem
MnBatchRpt)

Where I try to name the item (third line) I get the error message, "A
Component named MnBatchRptPrint already exists"

Any help would be appreciated.
--
Michael Lockey

Madness takes its toll.  Please have exact change.

Other Threads