Solved

dynamic menu

Posted on 1997-10-14
1
139 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
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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