Solved

How to populate a popup menu items with a query

Posted on 2002-05-07
7
432 Views
Last Modified: 2010-08-05
I want to populate a popup menu with the result of a query. The popup menu should dynamically create items as per the result of query (query1.Recordcount) and then display the result.

thnks
0
Comment
Question by:drnadeem
[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
7 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6993598
? trying from head (typos very possible)

var
  m : tmenuitem;
begin
  popupmenu1.items.clear;
  query1.first;
  while not query1.eof do
  begin
    m := tmenuitem.create(popupmenu1);
    m.caption := query1.fieldbyname('whatevername').AsString;
    m.onclick := AOnClickProcedureDefinedElseWhere;
    popupmenu1.items.add(m);
    query1.next;
  end;
end;
   

guessing i've mixed a bit with tlistitem, ttreenode,...
but somewhat in this way

meikl ;-)
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6993747
listening
0
 
LVL 3

Expert Comment

by:smurff
ID: 6993830
listening
0
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!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6993837
so much listeners :-))

if not solved within the next 4 hours,
then i will give a working sample

meikl ;-)
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
ID: 6994525
well,

a working sample

the popup is used to filter a table
(i used the table orders from the dbdemos-alias)
by the field shipvia

the popup-content based on a query

unit add_menu_items_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids, DBGrids, Menus, DBTables, Db;

type
  TForm1 = class(TForm)
    PopupMenu1: TPopupMenu;  //assigned to dbgrid, autohotkeys=maManual
    Table1: TTable;  //connected to dbdemos table orders
    DataSource1: TDataSource;
    Query1: TQuery;
    aa: TMenuItem;
    DBGrid1: TDBGrid;
    Button1: TButton;
    procedure PopupClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);  //Popup-onClick
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

//popup-click action
procedure TForm1.PopupClick(Sender: TObject);
var
  t : tdatetime;
  s : string;
begin
  if (sender is tmenuitem) then
  begin
    s :=copy(tmenuitem(sender).caption,1,pos(':',tmenuitem(sender).caption)-1);
    caption := 'Records shown : '+s;
    if s = '*' then
      table1.filtered := false
    else
    begin
      table1.Filter := 'shipvia = '+quotedstr(copy(tmenuitem(sender).caption,1,pos(':',tmenuitem(sender).caption)-1));
      table1.filtered := true;
    end;
  end;
end;


//build popupmenu
procedure TForm1.Button1Click(Sender: TObject);
var
  m : tmenuitem;
begin
  query1.close;
  query1.sql.text := 'select shipvia, count(*) anz from orders group by shipvia';
  query1.open;
  popupmenu1.Items.Clear;
  query1.first;
  while not query1.eof do
  begin
    m := tmenuitem.Create(self);
    m.Caption := query1.fieldbyName('shipvia').AsString+':'+
                 intToStr(query1.fieldbyname('anz').AsInteger);
    m.OnClick := PopupClick;
    popupmenu1.Items.Add(m);
    query1.next;
  end;
  //an item to resolve the filter
  m := tmenuitem.Create(self);
  m.Caption := '*:All';
  m.OnClick := PopupClick;
  popupmenu1.Items.Add(m);
end;

end.

hope this helps

meikl ;-)
0
 
LVL 3

Expert Comment

by:VSF
ID: 6996194
You can also download a working sample on this matter at http://www.victory.hpg.com.br/Samples/Menusdinamicos.zip

Hope it helps!
VSF
www.victory.hpg.com.br
www.boatoda.hpg.com.br
0
 
LVL 6

Expert Comment

by:Holger101497
ID: 6997452
well - I think this is solved.
Just my 2c worth: I kinda miss the word ".Free" here?!

I think  "popupmenu1.Items.Clear;" just removes the items from the menu but doesn't destroy them. Sooner or later you'll have thousands of TMenuItems "floating around" ;) (do they use any system resources other than memory??)

Have fun,
             Holger
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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 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