Recent

Author Topic: Adding event to a runtime component  (Read 628 times)

justnewbie

  • Sr. Member
  • ****
  • Posts: 273
Adding event to a runtime component
« on: March 02, 2021, 05:01:31 pm »
Guys, can you fix my code? Couldn't give an OnMouseUp event to it, doesn't compile.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls, LCLIntf;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     ColorDialog1: TColorDialog;    
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure ShapeMouseUp(Sender: TObject; Button: TMouseButton;
  19.       Shift: TShiftState; X, Y: Integer);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35.  
  36. procedure TForm1.Button1Click(Sender: TObject);   // Make empty palette
  37. var  shape: TShape;
  38. begin
  39.      shape := TShape.Create(Self);
  40.      shape.Parent := Form1;
  41.      shape.Left := 100;
  42.      shape.Top := 100;
  43.      shape.Width := 20;
  44.      shape.Height := 20;
  45.      shape.Pen.Width := 1;
  46.      shape.Pen.Color:=clBlack;
  47.      shape.Brush.Color:=clDefault;
  48.      shape.Visible := True;
  49.      shape.Enabled := True;
  50.  
  51.      shape.OnMouseUp := ShapeMouseUp;
  52. end;
  53.  
  54. procedure TForm1.ShapeMouseUp(Sender: TObject; Button: TMouseButton;
  55.   Shift: TShiftState; X, Y: Integer);
  56. begin
  57.    if ColorDialog1.Execute then
  58.    (Sender as TShape).Brush.Color:=ColorDialog1.Color;
  59. end;
  60.  
  61. end.


howardpc

  • Hero Member
  • *****
  • Posts: 3672
Re: Adding event to a runtime component
« Reply #1 on: March 02, 2021, 05:10:32 pm »
You need an "@" for objfpc mode
Code: Pascal  [Select][+][-]
  1. shape.OnMouseUp := @ShapeMouseUp;

justnewbie

  • Sr. Member
  • ****
  • Posts: 273
Re: Adding event to a runtime component
« Reply #2 on: March 02, 2021, 05:17:34 pm »
You need an "@" for objfpc mode
Code: Pascal  [Select][+][-]
  1. shape.OnMouseUp := @ShapeMouseUp;

Great, it works! Thank you!
BTW, why the @ needed here? For the OnClick it doesn't need.

Handoko

  • Hero Member
  • *****
  • Posts: 4140
  • My goal: build my own game engine using Lazarus
Re: Adding event to a runtime component
« Reply #3 on: March 02, 2021, 05:58:27 pm »
Which OnClick did you mean?

If you want to pass a procedure as a variable then you need a @ symbol. It is not needed in Delphi Mode or if you use TypeAddressOn compiler switch.

Why do we need it? Because the syntax defines it so. That actually is a good thing, it can prevent programmers to do some stupid things.

Lazarus/FPC/Pascal is verbose and strict. Luckily all the rules can be found if you check the documentation:

https://wiki.lazarus.freepascal.org/@
https://www.freepascal.org/docs-html/user/usersu88.html#x132-1390007.3.3
https://www.freepascal.org/docs-html/prog/progsu76.html
« Last Edit: March 02, 2021, 06:10:16 pm by Handoko »

justnewbie

  • Sr. Member
  • ****
  • Posts: 273
Re: Adding event to a runtime component
« Reply #4 on: March 02, 2021, 09:59:09 pm »
Thank you, Handoko!

 

TinyPortal © 2005-2018