Board index » delphi » **Question** Adding OnClick events at runtime

**Question** Adding OnClick events at runtime

Hi everyone, Newbie here

I try to write some code that lets me create a menu at run time. The
menuitem-text and onclick-actions should be run-time-definable.
Example : I type in two lines of text in edit boxes: The first one is
the menuitem-text (caption), the second is the path and filename that
has to be executed when clicking the menuitem.
After adding the menuitem to the menu, an onClick event should result
in executing the program from within my Main application.

The creation of the menuitems and the menutext at run time is not the
problem, but how do you create events at run time ?

Thanks,

Yogi Bear

 

Re:**Question** Adding OnClick events at runtime


Event is a property that stores !!!object!!! procedure's pointer.
Object procedure pointer is defined by

type
  TOProc = procedure of object;
// object procedure pointer keeps address of procedure and object together:

var
  H: TOProc;
  O: TOItself;
begin
  O := TOItself.Create;
  H := O.MyProc;
  H;
// O.H will be called.
// Using normal proc pointers we would have to use 'O.H' to specify objects
// explicitly.

So back to your case:
type
  Form1 = class(TForm)
    procedure TForm1.MyClickProc(Sender: TObject);
  end;

// property OnClick: TNotifyEvent;
// TNotifyEvent = procedure (Sender: TObject) of object;
var
  M: TMenuItem;

begin
  M :=
  ...
  M.OnClick := Form1.MyClickProc;
end;

Re:**Question** Adding OnClick events at runtime


Quote
In article <4nv2eb$...@news.xs4all.nl> yogib...@dds.nl (yogibear) writes:
>From: yogib...@dds.nl (yogibear)
>Subject: **Question**  Adding OnClick events at runtime
>Date: Wed, 22 May 1996 12:50:55 GMT
>Hi everyone, Newbie here
>I try to write some code that lets me create a menu at run time. The
>menuitem-text and onclick-actions should be run-time-definable.
>Example : I type in two lines of text in edit boxes: The first one is
>the menuitem-text (caption), the second is the path and filename that
>has to be executed when clicking the menuitem.
>After adding the menuitem to the menu, an onClick event should result
>in executing the program from within my Main application.
>The creation of the menuitems and the menutext at run time is not the
>problem, but how do you create events at run time ?

this is a bit of of a conudrum - D is a compiler, so while you can set a
menuitem to trigger a certain event, the ability to do whatever you want must
have been coded into the .exe. Assuming you have code for the event in the
unit, then you can just set the eventhandler name to the proc name with the
actions you want done eg

procedure twotsit.doathing(sender : tobject);

begin
  callaprocthatdoessomethinguseful;
end;

  menuitem1.onclick := doathing;

If you want user definable actions, you probably need a scripting language /
macro player, like hyperterp.

HTH

Boris Ingram, Cyborg Software
boris...@iafrica.com
Compuserve : 100076, 3616
http://www.pcb.co.za/users/borising/cyborg.htm

Other Threads