Solved

How to populate a popup menu items with a query

Posted on 2002-05-07
7
392 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
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

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

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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

14 Experts available now in Live!

Get 1:1 Help Now