Solved

TMenuItem ( onclick )

Posted on 1998-12-03
8
952 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
[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
  • 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
Technology Partners: 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 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

742 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