Solved

dynamic menu

Posted on 1997-10-14
1
144 Views
Last Modified: 2010-04-04
Hi,

I want to add dropdown menu's in my app at runtime. This works alright.
When one of the menu-items is clicked a procedure is called. But how do I know which menu item was selected,because they all call the same proc. How do I do this? I Can't link proc's to menu-items which don't yet exist in development.
0
Comment
Question by:lizzzard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 3

Accepted Solution

by:
Pegasus100397 earned 50 total points
ID: 1347572
Lizzzard,

A very good question! Got my gears working anyway. Here's what I figured out:

First, Create the menu items at runtime
(remember that popupmenu1 is initially empty)

procedure TForm1.Button1Click(Sender: TObject);
var NewItem: TMenuItem;
      iItemCount: Integer;
BEGIN

For iItemCount := 1 to 5 DO
  Begin
    NewItem := TMenuItem.Create(PopupMenu1);
    NewItem.Caption := 'Menu Item #' + IntToStr(iItemCount);
    PopupMenu1.Items.Insert(PopupMenu1.Items.Count,                                                NewItem);
{Now we assocate a pre-definded procedure for the menu item
 All menu items are assigned the same procedure}
    NewItem.OnClick := HandleMenuClick;
end;

Cool! Now here's the pre-defined procedure to handle the menu clicks:

procedure TForm1.HandleMenuClick(Sender: TObject);
var MI: TMenuItem;
BEGIN

MI := Sender as TMenuItem;
ShowMessage(You Clicked on the menu item called ( ' +
  TM.Caption + ' )');

{Here is where you put a bunch of IF statements to handle each menu item's stuff}

END;

Hope this helps!

Pegasus
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question