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
LVL 1
filipecorreiaAsked:
Who is Participating?
 
ei97001Connect With a Mentor Commented:
Try it like this...


void __fastcall  TForm1::MyFunction(TObject *Sender)
{
        TMenuItem *mi = new TMenuItem(PopupMenu2);
        mi = (TMenuItem*)  Sender;
        mi->Caption.....
}
0
 
aperdonCommented:
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....
0
 
WxWCommented:
TObject holds that information . Better way , use plain C++ instead of a Builder form .
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
filipecorreiaAuthor Commented:
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
0
 
aperdonCommented:
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..
0
 
filipecorreiaAuthor Commented:
Adjusted points to 70
0
 
filipecorreiaAuthor Commented:
If there is a property in TObject which I can use then I can't find it...

Help!!!

0
 
aperdonCommented:
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.
0
 
filipecorreiaAuthor Commented:
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 ..."
0
 
aperdonCommented:
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.
0
 
filipecorreiaAuthor Commented:
I've solved it out.
Thanks anyway...

Filipe
0
 
filipecorreiaAuthor Commented:
I already knew that. But thanks anway...
0
All Courses

From novice to tech pro — start learning today.