Solved

Sub-popupmenu of a popupmenu...

Posted on 2014-01-18
3
288 Views
Last Modified: 2014-01-19
Hi Experts... I have a popupmenu with several items dynamically populated in my app. I would like to have it so that when a user clicks on any of these items in the popupmenu, another very small popupmenu will appear showing always the same two entries (For example: Numeric, and Text). SO in other words, the user click an item from the first popup, then he needs to specify whether the item he clicked is a Numeric or Text value, so he does another selection accordingly from the 2nd popup that immediately appears. How to do this?

Thanks!
    Shawn
0
Comment
Question by:shawn857
  • 2
3 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 39791180
Use two PopupMenus. The first PopupMenu Items' OnClick fire the second one:

procedure TForm1.MenuItem1Click(Sender: TObject);
begin
  PopupMenu2.Popup(PopupMenu1.PopupPoint.X, PopupMenu1.PopupPoint.Y+24);  //Show the other menu a little bit beneath the first menu position
end;

Open in new window


Or use SubMenu.
0
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 39791262
Here is a complete example (Create new application, drop a listbox, doubleClick on your form to generate FormOnCreate, and then replace your unit with the following code):
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    PopMenu1: TPopupMenu;
    PopMenu2: TPopupMenu;

    // MenueItems OnClick Events
    procedure Show11Click(Sender: TObject);
    procedure Show21Click(Sender: TObject);
    procedure Numeric1Click(Sender: TObject);
    procedure Text1Click(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Show11Click(Sender: TObject);
begin
  PopMenu2.Tag := 1;
  PopMenu2.Popup(PopMenu1.PopupPoint.X, PopMenu1.PopupPoint.Y+24);
end;

procedure TForm1.Show21Click(Sender: TObject);
begin
  PopMenu2.Tag := 2;
  PopMenu2.Popup(PopMenu1.PopupPoint.X, PopMenu1.PopupPoint.Y+24);
end;

procedure TForm1.Numeric1Click(Sender: TObject);
begin
  if PopMenu2.Tag = 1 then  // to distinguish between which MenuItem is calling the second menu, just assign a flag or use the tag property
    begin
      Showmessage(IntToStr(3 + StrToInt(ListBox1.Items[ListBox1.ItemIndex])));
    end;

  if PopMenu2.Tag = 2 then
    begin
      Showmessage(IntToStr(4 + StrToInt(ListBox1.Items[ListBox1.ItemIndex])));
    end;
end;

procedure TForm1.Text1Click(Sender: TObject);
begin
  if PopMenu2.Tag = 1 then
    begin
      Showmessage(ListBox1.Items[ListBox1.ItemIndex]);
    end;

  if PopMenu2.Tag = 2 then
    begin
      Showmessage(ListBox1.Items[ListBox1.ItemIndex]);
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  Show11: TMenuItem;
  Show21: TMenuItem;
  Numeric1: TMenuItem;
  Text1: TMenuItem;
begin
  PopMenu1 := TPopupMenu.Create(Self);
  PopMenu2 := TPopupMenu.Create(Self);

  Show11 := TMenuItem.Create(PopMenu1);
  Show21 := TMenuItem.Create(PopMenu1);
  Show11.Caption := 'Show1';
  Show21.Caption := 'Show2';
  Show11.OnClick := Show11Click;
  Show21.OnClick := Show21Click;
  PopMenu1.Items.Add(Show11);
  PopMenu1.Items.Add(Show21);

  Numeric1 := TMenuItem.Create(PopMenu2);
  Text1 := TMenuItem.Create(PopMenu2);
  Numeric1.Caption := 'Numeric';
  Text1.Caption := 'Text';
  Numeric1.OnClick := Numeric1Click;
  Text1.OnClick := Text1Click;
  PopMenu2.Items.Add(Numeric1);
  PopMenu2.Items.Add(Text1);

  ListBox1.PopupMenu := PopMenu1;
  ListBox1.Items.Add('Item 1 Text');
  ListBox1.Items.Add('4');
  ListBox1.Items.Add('Item 2 Text');
  ListBox1.Items.Add('7');
  ListBox1.Items.Add('Item 3 Text');
end;

end.

Open in new window


It's just an idea, it could be improved.
0
 

Author Closing Comment

by:shawn857
ID: 39793430
Fantastic, thank you Jimy!

Ciao
  Shawn
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

11 Experts available now in Live!

Get 1:1 Help Now