Discussion:
OnClick event handler
(too old to reply)
JackM
2008-09-03 17:26:55 UTC
Permalink
Question:
How do I access an "OnClick" event handler for a dynamically create
menu option?
I have a TMenuItem "Display1" and have dynamically added subMenu items
"Victory x, Victory y, and Victory z" as submenu items under Display1.
Where do I find the event handler "OnClick" to use when the user
clicks one of the submenu items?

//////////////////////////////////////////////////////////////////////////////////////////////
//This part of the code is working:
//to dynamically add submenu items
//Victory x, Victory x, and Victory z, to "Display1":

// step one
// "Display1" was declared earlier using the Tools pallet and Object
inspector
// the declaration of "Display1" looks like this --> TMenuItem
*Display1;

// step two
TMenuItem *Itemx = new TMenuItem( Display1 );
TMenuItem *Itemy = new TMenuItem( Display1 );
TMenuItem *Itemz = new TMenuItem( Display1 );

// step three
Itemx->Caption="Victory x";
Itemy->Caption="Victory y";
Itemz->Caption="Victory z";


// step four
// included for clarity but not needed because Display1 is part of
MainMenu.
// MainMenu is declared as TMainMenu *MainMenu1;

// step five
// menu items added under menu item Display1 as subMenu items

Display1->Add(Itemx);
Display1->Add(Itemy);
Display1->Add(Itemz);

/////////////////////////////////////////////////////////////////////////////////////////
I greatly appreciate any hints.
Thank you,
Jack McDaniel 760 438 9255
John Kettle
2008-09-04 00:09:58 UTC
Permalink
In message
Post by JackM
How do I access an "OnClick" event handler for a dynamically create
menu option?
I have a TMenuItem "Display1" and have dynamically added subMenu items
"Victory x, Victory y, and Victory z" as submenu items under Display1.
Where do I find the event handler "OnClick" to use when the user
clicks one of the submenu items?
//////////////////////////////////////////////////////////////////////////////////////////////
//to dynamically add submenu items
// step one
// "Display1" was declared earlier using the Tools pallet and Object
inspector
// the declaration of "Display1" looks like this --> TMenuItem
*Display1;
// step two
TMenuItem *Itemx = new TMenuItem( Display1 );
TMenuItem *Itemy = new TMenuItem( Display1 );
TMenuItem *Itemz = new TMenuItem( Display1 );
// step three
Itemx->Caption="Victory x";
Itemy->Caption="Victory y";
Itemz->Caption="Victory z";
// step four
// included for clarity but not needed because Display1 is part of
MainMenu.
// MainMenu is declared as TMainMenu *MainMenu1;
// step five
// menu items added under menu item Display1 as subMenu items
Display1->Add(Itemx);
Display1->Add(Itemy);
Display1->Add(Itemz);
/////////////////////////////////////////////////////////////////////////////////////////
I greatly appreciate any hints.
Thank you,
Jack McDaniel 760 438 9255
need to create the event handler at the same time you dynamically create
the items following snippet to get you going ItemxOnClickHandler can be
any name you like to make up obviously two more needed for y and z only
shown one for x here.

void __fastcall TMainMenu1::showmenuClick(TObject *Sender)
{
TMenuItem *Itemx = new TMenuItem( Display1 );
Itemx->OnClick=ItemxOnClickHandler; // assign as the event handler

TMenuItem *Itemy = new TMenuItem( Display1 );
TMenuItem *Itemz = new TMenuItem( Display1 );
Itemx->Caption="Victory x";
Itemy->Caption="Victory y";
Itemz->Caption="Victory z";
Display1->Add(Itemx);
Display1->Add(Itemy);
Display1->Add(Itemz);
}

void __fastcall TMainMenu1::ItemxOnClickHandler(TObject *Sender)
{
//do something
Edit1->Text="itemx clicked";
}

and in the header file obviously add in public

public: // User declarations

void __fastcall ItemxOnClickHandler(TObject *Sender);
--
John Kettle
Loading...