Board index » delphi » Trapping OnClickEvent on a Dynamically added Menu Item

Trapping OnClickEvent on a Dynamically added Menu Item

Trapping OnClickEvent on a Dynamically added Menu Item
-------------------------------------------------------------------------------

I would like to add Dynamic menu Items.  

e.g.
       User Defined Report1
       User Defined Report2
       User Defined Report3
       User Defined Report4

Can anyone tell me how to trap the event where the user actually select one of
these menu items.

Brahm van Niekerk
Elgin Data Systems
bra...@iafrica.com

 

Re:Trapping OnClickEvent on a Dynamically added Menu Item


Brahm <bra...@iafrica.com>,

Quote
>I would like to add Dynamic menu Items.  

>e.g.
>       User Defined Report1
>       User Defined Report2
>       User Defined Report3
>       User Defined Report4

>Can anyone tell me how to trap the event where the user actually select one of
>these menu items.

  There are a couple of ways of doing this.  The easiest way is to create
these at Design-Time and pre-assign the OnClick event procedure.  You can
then change the caption for each menu item as needed:

  UserDefined1.Caption := 'User Defined Report1';
  UserDefined1.Visible := TRUE;
  UserDefined2.Caption := 'User Defined Report2';
  UserDefined2.Visible := TRUE;  
  UserDefined3.Caption := 'User Defined Report2';
  UserDefined3.Visible := FALSE;  {not defined}
  UserDefined4.Caption := 'User Defined Report2';
  UserDefined4.Visible := FALSE;  {not defined}

I would need to know more about your reports and menus to tell you what
the events would need to check to know how to actually run the reports,
but this can give you a start.

You can also dynamically assign the procedure if you need to.  Again,
that depends on how your program is set up.

Good Luck,

-Ed Salgado
 Eminent Domain Software

Re:Trapping OnClickEvent on a Dynamically added Menu Item


In article 000C4...@iafrica.com, bra...@iafrica.com (Brahm van Niekerk) writes:

Quote
>Trapping OnClickEvent on a Dynamically added Menu Item

>I would like to add Dynamic menu Items.  

>e.g.
>       User Defined Report1
>       User Defined Report2
>       User Defined Report3
>       User Defined Report4

>Can anyone tell me how to trap the event where the user actually select one of
>these menu items.

>Brahm van Niekerk
>Elgin Data Systems
>bra...@iafrica.com

NewItem:=TMenuItem.Create(MainMenu);
...
NewItem.OnClick:=MainForm.UserReportClick;

Procedure MainForm.UserReportClick(Sender : TObject);
Var
  I,iErr : Integer;
  S : String;
Begin
  S:=TMenuItem(Sender).Caption,;
  Val(Copy(S,Length(S),1),I, iErr);
  If iErr>0 Then
   error..
  Else
  Case I of

May work for you..
HTH

Ray Cramer
R...@Pol.ac.uk

Re:Trapping OnClickEvent on a Dynamically added Menu Item


bra...@iafrica.com (Brahm van Niekerk) wrote:

Quote
>Trapping OnClickEvent on a Dynamically added Menu Item
>-------------------------------------------------------------------------------
>I would like to add Dynamic menu Items.  
>e.g.
>       User Defined Report1
>       User Defined Report2
>       User Defined Report3
>       User Defined Report4
>Can anyone tell me how to trap the event where the user actually select one of
>these menu items.
>Brahm van Niekerk
>Elgin Data Systems
>bra...@iafrica.com

Here is a procedure, lifted out of an example testbed I used to figure out how
to do dynamic popup menus....
Note the line 'MItem.OnClick :=<procedure name>
This will assign a handler to the Menu item OnClick event at run time.
I could send you the complete code, if it would be helpful.

procedure TForm1.MakeAMenu(Sender : TObject; P : TPoint);

var
   MItem : TMenuItem;
   ndx : integer;

begin
  DynPopUp := TPopUpMenu.Create(Self);
  MItem := TMenuItem.Create(DynPopUp);
  MItem.Caption := '&Color...';
  MItem.OnClick := GetAColor;
  DynPopUp.Items.Add(MItem);
  MItem := TMenuItem.Create(DynPopUp);
  MItem.Caption := '&Font...';
  DynPopUp.Items.Add(MItem);
  if (Sender as TWinControl).Name = 'CheckBox1' then
  begin
    MItem := TMenuItem.Create(DynPopUp);
    MItem.Caption := '-';
    DynPopUp.Items.Add(MItem);
    MItem := TMenuItem.Create(DynPopUp);
    MItem.Caption := 'Chec&k it';
    DynPopUp.Items.Add(MItem);
    MItem := TMenuItem.Create(DynPopUp);
    MItem.Caption := 'Clea&r if';
    DynPopUp.Items.Add(MItem);
    MItem := TMenuItem.Create(DynPopUp);
    MItem.Caption := '&Disable It';
    DynPopUp.Items.Add(MItem);
  end;
  if (Sender as TWinControl).Name = 'ListBox1' then
  begin
    MItem := TMenuItem.Create(DynPopUp);
    MItem.Caption := '-';
    DynPopUp.Items.Add(MItem);
    MItem := TMenuItem.Create(DynPopUp);
    MItem.Caption := 'List &Box Stuff';
    MItem.OnClick := DefaultClicker;
    DynPopUp.Items.Add(MItem);
    case (Sender as TListBox).ItemIndex of
      1 : begin
            MItem := TMenuItem.Create(DynPopUp);
            MItem.Caption := 'Item &2 Specific';
            MItem.OnClick := DefaultClicker;
            DynPopUp.Items.Add(MItem);
          end;
      2 : begin
            MItem := TMenuItem.Create(DynPopUp);
            MItem.Caption := 'Item 3 part 1';
            MItem.OnClick := DefaultClicker;
            DynPopUp.Items.Add(MItem);
            MItem := TMenuItem.Create(DynPopUp);
            MItem.Caption := 'Item 3 part 2';
            MItem.OnClick := DefaultClicker;
            DynPopUp.Items.Add(MItem);
          end;
    end; { CASE }
  end;
  if (Sender as TWinControl).Name = 'Outline1' then
  begin
    MItem := TMenuItem.Create(DynPopUp);
    MItem.Caption := '-';
    DynPopUp.Items.Add(MItem);
    MItem := TMenuItem.Create(DynPopUp);
    MItem.Caption := '&Delete';
    MItem.OnClick := DefaultClicker;
    DynPopUp.Items.Add(MItem);
    MItem := TMenuItem.Create(DynPopUp);
    MItem.Caption := '&Edit';
    MItem.OnClick := DefaultClicker;
    DynPopUp.Items.Add(MItem);
    ndx := (Sender as TOutline).SelectedItem;
    MItem := TMenuItem.Create(DynPopUp);
    MItem.Caption := '-';
    DynPopUp.Items.Add(MItem);
    case (Sender as TOutline)[ndx].Level of
      1 : begin
            MItem := TMenuItem.Create(DynPopUp);
            MItem.Caption := 'Do Parent Stuff';
            MItem.OnClick := DefaultClicker;
            DynPopUp.Items.Add(MItem);
            MItem := TMenuItem.Create(DynPopUp);
            MItem.Caption := 'More Parent Stuff';
            DynPopUp.Items.Add(MItem);
          end;
      2 : begin
            MItem := TMenuItem.Create(DynPopUp);
            MItem.Caption := 'Child Stuff';
            MItem.OnClick := DefaultClicker;
            DynPopUp.Items.Add(MItem);
          end;
      3 : begin
            MItem := TMenuItem.Create(DynPopUp);
            MItem.Caption := 'GrandKid Stuff';
            DynPopUp.Items.Add(MItem);
            MItem := TMenuItem.Create(DynPopUp);
            MItem.Caption := 'More Grandkids';
            DynPopUp.Items.Add(MItem);
          end;
    end; { CASE }
  end;
  DynPopup.Popup(P.X,P.Y);
end;

Re:Trapping OnClickEvent on a Dynamically added Menu Item


If you find out please Mail me or post to this News Group.

Thanks Huet Bartels

Re:Trapping OnClickEvent on a Dynamically added Menu Item


Quote
Huet Bartels <Hu...@MDX.AC.UK> wrote:
>If you find out please Mail me or post to this News Group.
>Thanks Huet Bartels

The rest of this thread has scrolled off of my list, so I'm guessing
what you are trying to accomplish - I did this a while back, so I'm
going from memory here!

when you create the new object, you simply need to point its onClick
method to an existing method ie:

        obNew.OnClick := obExisting.onClick;

{where obNew is the new object and obExisting is an existing object}

You'll want to trap the sender value in the existing handler to
complete the desired task.

---------------------------------------------
Opinions expressed do not necessarily reflect
those of my employer.

Steve Davidson
sdpi...@wolfenet.com

Para/Matrix Solutions, Inc.
Specialists in Application Development
and Training
---------------------------------------------

Re:Trapping OnClickEvent on a Dynamically added Menu Item


In article <4a6h3g$...@seva.mdx.ac.uk>, Hu...@MDX.AC.UK says...

Quote

>If you find out please Mail me or post to this News Group.

Based on the Subject, I believe the original question is:

Quote
> How do I trap the OnClick Event for a Dynamic Menu Item?

The answer is:

  MyMenuItem := TMenuItem.Create(Menu1);
  MyMenuItem.OnClick := OtherMenu.OnClick; {One Way}
     {OR}
  MyMenuItem.OnClick := SpecialClick;      {Another Way}

Where

Procedure SpecialClick(Sender: TObject);
Begin
  {Do Something}
End;

HTH,

Kirk Out

Other Threads