Solved

Sub-popupmenu of a popupmenu...

Posted on 2014-01-18
3
307 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

Technology Partners: 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

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 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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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 …

713 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