Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

Sub-popupmenu of a popupmenu...

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
shawn857
Asked:
shawn857
  • 2
1 Solution
 
jimyXCommented:
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
 
jimyXCommented:
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
 
shawn857Author Commented:
Fantastic, thank you Jimy!

Ciao
  Shawn
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now