Solved

dynamic menu

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

617 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