Link to home
Start Free TrialLog in
Avatar of filipecorreia
filipecorreiaFlag for Portugal

asked on

Creating popup menus at runtime using C++ Builder 4

I'm using a system tray icon in witch a popup menu will appear. I want to generate the items in this menu at runtime. I'm using the following code:


for(int i=0;i<50;i++){
   if(data[i]!=NULL){
     TMenuItem *mi1 = new TMenuItem(PopupMenu2);
     mi1->Caption=data[i];
     PopupMenu2->Items->Add(mi1);
     PopupMenu2->Items->Items[i]->OnClick = MyFunction;
    }
}
void __fastcall  TForm1::MyFunction(TObject *Sender)
{
ShowMessage("MyFunction Executed");
}


Note: The items that will apear on the menu are stored in data[] witch is a 50 element AnsiString array with some NULL elements(that aren't supose to appear in the menu).


This code works fine (the menu I want does apear) but I'd like to know witch item in the menu was clicked when the "MyFunction" code is executed.

I'm using BCB4.

Filipe
Avatar of aperdon
aperdon

Just specify for the different items in the menu a different callback-function.
MyFunc1.....MyFunc50

I am not used in BC4, but i assume there is a property which you can use as to set an identifier. Or maybe you can get the index of the selected item.
go in this direction....
TObject holds that information . Better way , use plain C++ instead of a Builder form .
Avatar of filipecorreia

ASKER

TO APERDON:

I don't want to create 50 different function because
-> I intend to add more items in the future and that would imply adding more and more functions(definitely it wouldn't be very good programming...)

->Really, I don't know how to do it. I haven't tried but I don't think something like this would work:

PopupMenu2->Items->Items[i]->OnClick = MyFunction + i;

Is it possible to define a function at runtime?

Filipe







TO WxW:

I guess you mean " *Sender " when you mention TObject but how can I acces that information?
Sender->???????

Filipe
For me I dont know if it possible to create functions runtime. Never done it. But I think it is not possible.
About the 50 functions is just a joke.

There must be a property in TObject which can be used to determine which object it is. There has to be..
Adjusted points to 70
If there is a property in TObject which I can use then I can't find it...

Help!!!

But anyway, if the all items have equal functionality, why are you interested in which was the selected item. You could use caption or so.
Send me the help op TObject, maybe I can help you then.

Alex.
Not all Items have equal functionality. The function I called "MyFucnction" won't simply do a :

ShowMessage("MyFunction Executed");

It will act differentlu acording to the pressed item. If it would be possible I would use the index "i" that I use, but after defining "OnClick = MyFunction" I no longuer have access to "i" from "MyFunction".


What do you mean by: "help op ..."
help of TObject.

If you act differently you better use different functions. It is a bad practice in OOP to check dynamically which behaviour should be used, while this behaviour never changes during runtime.
I've solved it out.
Thanks anyway...

Filipe
ASKER CERTIFIED SOLUTION
Avatar of ei97001
ei97001

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I already knew that. But thanks anway...