Solved

TMenuItem ( onclick )

Posted on 1998-12-03
8
949 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
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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…

756 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