Solved

TMenuItem ( onclick )

Posted on 1998-12-03
8
946 Views
Last Modified: 2006-11-17
Hello Experts...

Here is my question...
------------------------------------------------------------
How can i make a TMenuItem And Assign Any Text To The Caption in for loop and execute any of the text with form1.caption on onclick event for the TMenuItem or Popupmenu -- lets say i click on the right mouse button and after that the menu will popup and when i click any of the item then i will show the caption of the item in form1.caption...Dem this is difficult for me.. thanks!!!
0
Comment
Question by:mummi
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1349191
Hi mummi,

this should it be :

assign the onclick events of the menuitems to the menuclick event :

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus;

type
  TForm1 = class(TForm)
    PopupMenu1: TPopupMenu;
    TEST11: TMenuItem;
    TEST21: TMenuItem;
    TEST31: TMenuItem;
    procedure MenuClick(Sender: TObject);    
  private
    { Private declarations }

  public
    { Public declarations }

  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.MenuClick(Sender: TObject);
begin
 Form1.Caption := (Sender as TMenuItem).Caption;
end;

end.

Regards, Zif.
0
 

Author Comment

by:mummi
ID: 1349192
I want to use the OnClick event on each item in the popupmenu
The menu have 3 items and let's say i will push on the third item text there is Paintbrush
and when i click the item i will execute the PaintBrush.... I 'am not building menu from popupmenu editor , i will do it by my self....

Can i use OnChange Event for the popupmenu or OnClick or ?
I'm stop

I hope you will
understand this?
Thanks!!!!!!
0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1349193
mummi

try this

procedure TForm1.MyPopupHandler(Sender: TObject);
begin
  with Sender as TMenuItem do begin
    ShellExecute(Handle, 'open', Caption, '' ,'directory here', 0);
     Form1.Caption := Caption;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
 MyPopUpItems: array[0..3] of TMenuItem;
begin
  for i := 0 to 3 do begin
    MyPopUpItems[i] := TMenuItem.Create(Self);
    MyPopUpItems[i].Caption := 'New item ' + IntToStr(i);
    MyPopUpMenu.Items.Add(MyPopUpItems[i]);
    MyPopUpItems[i].OnClick := MyPopupHandler;
  end;
end;

Later
BoRiS
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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 3

Accepted Solution

by:
Ronald Buster earned 100 total points
ID: 1349194
Hi Mummi,

I think this is just what you want enjoy.

regards,

Cono

<---->

declare into TForm1 protected section

  procedure PopupHandler(Sender: TObject);

declare into TForm1 private section

  fPopUpMenu : TPopUpMenu;
  procedure InitPopUpMenu;

procedure TForm1.InitPopUpMenu;
var TM : TMenuItem;
    Ts : String;
    i  : Integer;
begin
  fPopUpMenu := TPopUpMenu.Create(Self);
  for i := 1 to 5 do
    begin
      TM         := TMenuItem.Create(Self);
      TM.Caption := 'Item ' + IntToStr(i);
      TM.Tag     := SED_CONST_QFP+(i-1);
      TM.OnClick := PopupHandler;
      fPopUpMenu.Items.Add(TM);
    end;
end;

procedure TForm1.PopupHandler(Sender: TObject);
begin
  with Sender as TMenuItem do
    begin
        Form1.Caption := Caption;
    end;
end;

procedure TForm1.Create(Sender : TObject);
begin
   PopUpMenu := fPopUpMenu;
end;

<---->
0
 
LVL 3

Expert Comment

by:Ronald Buster
ID: 1349195
Hi Mummi,

I forgot something

in the form create procedure

you have to call initpopupmenu, before assigning fPopupmenu to the forms popupmenu

regards,

Cono

0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1349196
cono

Hmmm looks like what I said in my comment :-) non the less

Later
BoRiS
0
 

Author Comment

by:mummi
ID: 1349197
Adjusted points to 100
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1349198
like I said in my comment :

assign the onclick events of the menuitems to the menuclick event.

i.e. all menu items when clicked use menuclick procedure.

And everything should work...

In fact it's the same as cono's answer (only cono wrote it all in code ie. creation of menu) ... but heck...

Zif.

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 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