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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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
ei97001Commented:
Try it like this...


void __fastcall  TForm1::MyFunction(TObject *Sender)
{
        TMenuItem *mi = new TMenuItem(PopupMenu2);
        mi = (TMenuItem*)  Sender;
        mi->Caption.....
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
filipecorreiaAuthor Commented:
I already knew that. But thanks anway...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.