C++ Builder

La Isla Bonita



Popup Menu: Adding Menu Items Dynamically
Popup Menu: Adding Menu Items Dynamically


Description

This article explains how to create a TPopupMenu at runtime. The Popup Menu appears when the user clicks on the form (in general the Popup menu could be on a control) with the Right mouse button. All The components on the form are created dynamically.

When the program starts the popup menu is created by the form's constructor TForm1().
Then 10 menu items are created. Each menu item is added by the line:

PopupMenu1->Items->Add( Item[i] ); 
inside the for loop.
When the user presses the TButton, 10 additional menu items are inserted in the Popup menu after the third menu item. Each menu item is inserted by the function call:
PopupMenu1->Items->Insert( 3, Item[i] );


PopupAddMenuItem.cpp

#include <vcl.h>
#pragma hdrstop

#include "PopupAddMenuItem.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TPopupMenu* PopupMenu1;

const int DIM_MENUITEM = 20;
TMenuItem* Item[DIM_MENUITEM];

String ItemName[DIM_MENUITEM] = { "Anna","Teresa","Martha", "Rita", "Mary",
                                  "Cristina","Susana","Alicia", "Patricia", 
								  "Linda"};

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{

    TButton* Button1 = new TButton( this );
    Button1->Parent  = this;
    Button1->Top     = 100;
    Button1->Height  = 50;
    Button1->Width   = 100;
    Button1->Left    = (Width - Button1->Width)/2;
    Button1->Caption = "Button1";
    Button1->OnClick = Button1Click;
    
      //Create PopupMenu
    PopupMenu1 = new TPopupMenu( this );
    PopupMenu1->AutoPopup = true;
    PopupMenu1->TrackButton = tbRightButton;

      //Form's parameters initialization
    PopupMenu = PopupMenu1;
    Color     = (TColor)0x12a78345;
    Width     = 800;
    Height    = 600;
    
      //Create Menu Items
    for ( int i=0; i< DIM_MENUITEM/2; i++ )
    {
        Item[i] = new TMenuItem( PopupMenu1 );
        Item[i]->Caption = ItemName[i];
        PopupMenu1->Items->Add( Item[i] );
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    static int count = 0;
      //Create Menu Items
    for ( int i=DIM_MENUITEM/2; i< DIM_MENUITEM; i++ )
    {
        Item[i] = new TMenuItem( PopupMenu1 );
        Item[i]->Caption = "new item" + String( ++count );
        PopupMenu1->Items->Insert( 3, Item[i] );
    }

    String S1 = "The Popup Menu has ";
    String S2 = " Menu Items";
    ShowMessage( S1 + String(PopupMenu1->Items->Count) + S2 );
}

PopupAddMenuItem.h

#ifndef PopupAddMenuItemH
#define PopupAddMenuItemH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Menus.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};

extern PACKAGE TForm1 *Form1;

#endif
Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.