Solved

How to populate a popup menu items with a query

Posted on 2002-05-07
7
424 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TEMBEDDEDWB how can i change its user agent ? 8 103
Dynamically Created Query 3 78
Firemonkey android show image from resource ? 1 69
Delphi Seattle StructureView color 1 32
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

737 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