[SOLVED] Set OnClick event on dynamically created button
lazarus / 21-General
----------------------------------------------------------------------
Lurgainn · 2024-12-06 15:43
Sorry, I know that this is a already disccussed argument, but I really
need a tip because I'm not able to understand what I'm doing wrong.
I created a unit WITHOUT any form (so no class exists), to put inside
all necessary code to dynamically create a new form with some
components, one of them is a button, and obviously I want to assign a
procedure to this button.
This unit will be used from other units that have a form, by insert
"UnitFormDeluxeAbout" into uses, and calling the procedure
"CreateFormDeluxeAbout" and assigning tha calling form as parent.
My test code is this:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i unit
UnitFormDeluxeAbout; {$mode ObjFPC}{$H+} interface uses Classes,
SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Buttons,
StdCtrls; procedure CreateFormDeluxeAbout(form_parent:
TForm); implementation var DeluxeFormAbout: TForm;
DeluxeFormAboutCloseButton: TButton; procedure
DeluxeFormAboutCloseButtonClick(Sender: TObject); forward; procedure
CreateFormDeluxeAbout(form_parent: TForm);begin DeluxeFormAbout :=
TForm.Create(form_parent); DeluxeFormAbout.SetBounds(100, 100, 220,
150); DeluxeFormAbout.Caption := 'My dynamic created form';
DeluxeFormAbout.Parent := form_parent; DeluxeFormAboutCloseButton
:= TButton.Create(DeluxeFormAbout);
DeluxeFormAboutCloseButton.Caption := 'Close my form';
DeluxeFormAboutCloseButton.SetBounds(10, 10, 200, 30);
DeluxeFormAboutCloseButton.Parent := DeluxeFormAbout;
DeluxeFormAboutCloseButton.OnClick :=
@DeluxeFormAboutCloseButtonClick; DeluxeFormAbout.ShowModal;
FreeAndNil(DeluxeFormAbout);end; procedure
DeluxeFormAboutCloseButtonClick(Sender: TObject);begin
ShowMessage('Attention, closing my dynamically created form!'); if
Sender is TButton then
TForm(TButton(Sender).Parent).Close;end; end.
But when I build this unit, I get this error:
unitformdeluxeabout.pas(33,43) Error: Incompatible types: got
"
" expected ""
Is there a simple way to solve this problem?
Thanks so much for any help
----------------------------------------------------------------------
cdbc · 2024-12-06 16:09
Hi
I dunno if it's simple, but here goes:Code: Pascal
[Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i unit
UnitFormDeluxeAbout; {$mode ObjFPC}{$H+} interface uses Classes,
SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Buttons,
StdCtrls; procedure CreateFormDeluxeAbout(form_parent:
TForm); implementationtype THack = object procedure
DeluxeFormAboutCloseButtonClick(Sender: TObject); end; var
DeluxeFormAbout: TForm; DeluxeFormAboutCloseButton: TButton;
EventH: THack; procedure CreateFormDeluxeAbout(form_parent:
TForm);begin DeluxeFormAbout := TForm.Create(form_parent);
DeluxeFormAbout.SetBounds(100, 100, 220, 150);
DeluxeFormAbout.Caption := 'My dynamic created form';
DeluxeFormAbout.Parent := form_parent; DeluxeFormAboutCloseButton
:= TButton.Create(DeluxeFormAbout);
DeluxeFormAboutCloseButton.Caption := 'Close my form';
DeluxeFormAboutCloseButton.SetBounds(10, 10, 200, 30);
DeluxeFormAboutCloseButton.Parent := DeluxeFormAbout;
DeluxeFormAboutCloseButton.OnClick :=
@EventH.DeluxeFormAboutCloseButtonClick;
DeluxeFormAbout.ShowModal;
FreeAndNil(DeluxeFormAbout);end; procedure
THack.DeluxeFormAboutCloseButtonClick(Sender: TObject);begin
Application.ShowMessage('Attention, closing my dynamically created
form!'); if Sender is TButton then
TForm(TButton(Sender).Parent).Close;end; end. Quick'n'Dirty ...but
something along these lines
Regards Benny
----------------------------------------------------------------------
Lurgainn · 2024-12-06 17:18
Thanks so much !!!!
I had no idea that the OnClick procedure must be defined as an
Object's method.
----------------------------------------------------------------------
Remy Lebeau · 2024-12-06 17:30
Quote from: cdbc on December 06, 2024, 05:09:33 pmCode: Pascal
[Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i var
DeluxeFormAbout: TForm; DeluxeFormAboutCloseButton: TButton;
EventH: THack;
You are not creating the EventH object, so
DeluxeFormAboutCloseButtonClick() method will be called on an invalid
object. This can be find if you never access the method's Self
parameter, but it is something to be aware of.
Quote from: Lurgainn on December 06, 2024, 06:18:18 pmI had no idea
that the OnClick procedure must be defined as an Object's method.
Technically, you CAN use a standalone procedure, butt you have to
tweak the approach a little:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i procedure
DeluxeFormAboutCloseButtonClick(Self: Pointer; Sender: TObject);
forward; procedure CreateFormDeluxeAbout(form_parent: TForm);var
ClickHandler: TNotifyEvent;begin ... TMethod(ClickHandler).Code
:= @DeluxeFormAboutCloseButtonClick; TMethod(ClickHandler).Data :=
...; // whatever you want the Self parameter to point at...
DeluxeFormAboutCloseButton.OnClick := ClickHandler; ... end;
procedure DeluxeFormAboutCloseButtonClick(Self: Pointer; Sender:
TObject);begin ...end;
----------------------------------------------------------------------
cdbc · 2024-12-06 17:54
Hi Remy
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i type THack
= object procedure DeluxeFormAboutCloseButtonClick(Sender:
TObject); end;It's NOT a class!
Regards Benny