Board index » cppbuilder » TPopupMenu that's constructed during runtime problem

TPopupMenu that's constructed during runtime problem


2006-01-23 04:57:34 PM
cppbuilder63
Hi all,
I have a TPopupMenu that I have created dring runtime, with it's menu
items/sub-items loaded according to some list.
i.e. I have:
Location--->North--->Ba{*word*224}t1
| |->Ground
| |->Level1
| |->Level2
|
|->South->...
|->East->...
|->West->...
(please view with fixed size font like Courier if appeared displaced)
the North, South, East, West and Ba{*word*224}t1, Ground, Level1, Level2 are
loaded from a list files and added to the menu at runtime. Naturally, these
sub-items do not have their own names to refer to. So if I pop the menu and
clicked on Level2, how do I know it's clicked and how do I assign an event
handler to it? Is there any way I can track the whole link that the user
clicked (in this case the link will be Location->North->Level2)? The actual
lists loaded in to the Popup Menu is much larger and contains more levels of
sub-items than the example.
The reason I want to do this is I have an app to allow the user to search
the location and other criterias of an object via the popup once the user
clicked on one of the lowest level item (the top level menu items being the
roots of each branches) and I need to put the lists (or the Keywords) in a
text file so that the user can update/expand the list by editing the text
file. It is until I'm actually going to construct an SQL search statement
(i.e. SELECT * FROM ObjectsDB WHERE Location = 'North' AND Level = 'Level2')
that I realize I can't tell which route the user selected and where to
assign the Handler to. Is there a way that I could identified the one item
that is clicked and back track the higher level items so I can get the route
of the selected item? Please help. T.I.A.
regards.
 
 

Re:TPopupMenu that's constructed during runtime problem

"blue" < XXXX@XXXXX.COM >wrote in message
Quote
So if I pop the menu and clicked on Level2, how do I know
it's clicked and how do I assign an event handler to it?
Simply assign the OnClick event dynamically at the time that you create the
menu item, ie:
TMenuItem *North = new TMenuItem(Location1);
North->Caption = "North";
//...
Location1->Add(North);
//...
TMenuItem *Level2 = new TMenuItem(North);
Level2->Caption = "Level2";
Level2->OnClick = Level2Clicked;
//...
North->Add(Level2);
Where Level2Clicked() is a method of your form:
private:
void __fastcall Level2Clicked(TObject *Sender);
void __fastcall TForm1::Level2Clicked(TObject *Sender)
{
// do something when Level2 is clicked ...
}
Quote
Is there any way I can track the whole link that the user clicked
(in this case the link will be Location->North->Level2)?
In the event handler, the Sender parameter will point to the TMenuItem that
was clicked on. You can cast that to a TMenuItem* pointer and then use its
Parent property to traverse the hierarchy as needed.
Quote
Is there a way that I could identified the one item that is clicked
That is what the Sender parameter of the event handler is for.
Quote
and back track the higher level items
That is what the TMenuItem::Parent property is for.
Gambit
 

Re:TPopupMenu that's constructed during runtime problem

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote
in:43d5171f$ XXXX@XXXXX.COM ...
Quote
<snipped>

Thanks for the reply. I should have thought of that, :).
Anyway, after giving some thought to the method, I realised that it is not
very felxible, I need to pre-write the event handlers anyway, so this defies
my intention of making the Popup Menu expendable/customable. My intention is
to write a program where the user can change/expand the menu by just adding
the item to the list (since all the items are simply keywords for
constructing the search statement, and the click is simply needed to trigger
the event of construction of the statement and calling Query->Open(). Is it
possible that I write a generic handler like:
void __fastcall TForm1::myOnClick(TObject *Sender)
{
//from the Sender, back track all the keywords
...
//construct the SQL statement with the keywords
...
//do the query
...
}
and assign it to ALL the lowest level items like
PopuoMenu->Items->Items[0]->Items[0]->OnClick = myOnClick;//Ba{*word*224}t
PopuoMenu->Items->Items[0]->Items[1]->OnClick = myOnClick;//Ground
PopuoMenu->Items->Items[0]->Items[2]->OnClick = myOnClick;//Level1
PopuoMenu->Items->Items[0]->Items[3]->OnClick = myOnClick;//Level2
is this workable?
Thanks.
Regards.
 

{smallsort}

Re:TPopupMenu that's constructed during runtime problem

"blue" < XXXX@XXXXX.COM >wrote:
Quote

[...] I realised that it is not very felxible, I need to pre-
write the event handlers anyway, so this defies my intention
of making the Popup Menu expendable/customable.
You are mistaken.
Quote
My intention is to write a program where the user can
change/expand the menu by just adding the item to the list
(since all the items are simply keywords for constructing
the search statement, and the click is simply needed to
trigger the event of construction of the statement and
calling Query->Open(). Is it possible that I write a generic
handler like:
Yes it is. All you have to do is associate the keyword with the
particular TMenuItem which can be done a number of ways. For
example, you could incorportate the keyword into the Caption
so that as you traversed backwards, you would extract it and
construct the query.
You could also use a TStringList of fully constructed queries
that you put together as you allocate the TMenuItems and simply
assign the index for each query to the Tag property of the
TMenuItem.
Then there's variouse combinations of both methods or multiple
TStringList's or dynamically allocating memory for strings of
keywords that are assigned to the Tag property.
~ JD