Set onclick event at runtime in Delphi XE -


first post here please forgive etiquette blunders.

i'm creating multi-device (fmx) app in delphi xe8 , having difficulty assigning event handler dynamically created button. searched through stackoverflow , found answers relating notifyevents followed advice in answers - still no luck. compilation error "e2010 incompatible types: 'tnotifyevent' , 'procedure'".

i've put simple test case of form edit field , static hello button, 2nd button creates goodbye button , tries assign procedure onclick event i'm still getting same error.

as far can tell i've followed of requirements of making procedure compatible tnotifyevent basic example fails same error. i'm banging head against wall can please let me know i've done wrong?

many thanks.

unit dynamic_button_test1;  interface  uses   system.sysutils, system.types, system.uitypes, system.classes, system.variants,   fmx.types, fmx.controls, fmx.forms, fmx.graphics, fmx.dialogs, fmx.stdctrls,   fmx.controls.presentation, fmx.edit;  type   tform1 = class(tform)     edit1: tedit;     hello: tbutton;     create_goodbye: tbutton;     procedure helloclick(sender: tobject);     procedure create_goodbyeclick(sender: tobject);   private     { private declarations }   public     { public declarations }     procedure goodbyeclick(sender: tobject) ;   end;  var   form1: tform1;  implementation  {$r *.fmx}  procedure tform1.create_goodbyeclick(sender: tobject); var   new_button : tbutton ; begin new_button := tbutton.create( form1 ); new_button.parent := form1 ; new_button.text := 'goodbye' ; new_button.visible := true ; new_button.margins.left := 50 ; new_button.margins.right := 50 ; new_button.margins.bottom := 30 ; new_button.height := 50 ; new_button.align := talignlayout.bottom ;  new_button.onclick := tform1.goodbyeclick ; end;  procedure tform1.helloclick(sender: tobject); begin edit1.text := 'hello' ; end;  procedure tform1.goodbyeclick(sender: tobject); begin edit1.text := 'goodbye' ; end;  end. 

vcl/fmx event handlers tied specific objects @ runtime. when assigning event handler, need replace class typename object pointer. object event handler's self pointer when event triggered later on:

new_button.onclick := self.goodbyeclick ; 

or simply:

new_button.onclick := goodbyeclick ; // self implicitly used 

on side note - when creating button, code inside of tform1 instance method, should using self object pointer instead of global form1 object pointer:

new_button := tbutton.create( self ); new_button.parent := self ; 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -