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
Solved

Sub-popupmenu of a popupmenu...

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 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