Board index » cppbuilder » How to dynamically create/edit/destroy TPopupMenu menu items

How to dynamically create/edit/destroy TPopupMenu menu items

How can you dynamically create/edit/destroy TPopupMenu menu items?

Mark
[mkol...@hotmail.com]
[http://majiknet.freeservers.com]
[MajiK6pt5 on most anything]

 

Re:How to dynamically create/edit/destroy TPopupMenu menu items


Oh yeah, I'm using BCB 1 Proffesional.

Mark
[mkol...@hotmail.com]
[http://majiknet.freeservers.com]
[MajiK6pt5 on most anything]

Re:How to dynamically create/edit/destroy TPopupMenu menu items


Hi Mark,

Quote
> How can you dynamically create/edit/destroy TPopupMenu menu items?

To create a new TMenuItem, simply call its constructor, passing an
appropriate owner.  Then, use the TMenuItem::Add() member function
through the PopupMenu's Items property.  For example...

// in header...
    void __fastcall PopupMenuItemsClick(TObject *Sender);

// in source...
void __fastcall TForm1::AddButtonClick(TObject *Sender)
{
    const int num_items = 4;
    for (int index = 0; index < num_items; ++index)
    {
        TMenuItem *NewItem = new TMenuItem(PopupMenu1);
        PopupMenu1->Items->Add(NewItem);
        NewItem->Caption = "Menu Item " + IntToStr(index);
        NewItem->Tag = index;
        NewItem->OnClick = PopupMenuItemsClick;
    }

Quote
}

void __fastcall TForm1::PopupMenuItemsClick(TObject *Sender)
{
    TMenuItem *ClickedItem = dynamic_cast<TMenuItem *>(Sender);
    if (ClickedItem)
    {
        switch (ClickedItem->Tag)
        {
            case 0:
            {
                ShowMessage("first item clicked");
                break;
            }
            case 1:
            {
                ShowMessage("second item clicked");
                break;
            }
            // etc...
        }
    }

Quote
}

To edit or destroy an item, grab its pointer via the TMenuItem::Items
property...

void __fastcall TForm1::EditButtonClick(TObject *Sender)
{
    const int index = 1;
    TMenuItem *ItemToEdit = PopupMenu->Items->Items[index];
    ItemToEdit->Caption = "Changed Caption";

Quote
}

void __fastcall TForm1::DestroyButtonClick(TObject *Sender)
{
    const int index = 2;
    TMenuItem *ItemToDelete = PopupMenu->Items->Items[index];
    delete ItemToDelete;

Quote
}

Good luck!

--
Damon Chandler
http://bcbcaq.freeservers.com

Other Threads