Solved

dynamic menu

Posted on 1997-10-14
1
130 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
1 Comment
 
LVL 3

Accepted Solution

by:
Pegasus100397 earned 50 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now