Solved

How to populate a popup menu items with a query

Posted on 2002-05-07
7
415 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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