Recent

Author Topic: [SOLVED] Assigning double click to shape at runtime?  (Read 425 times)

AsleyCruz

  • Jr. Member
  • **
  • Posts: 78
    • Graphic and apps designer
[SOLVED] Assigning double click to shape at runtime?
« on: April 13, 2020, 01:16:29 am »
Hi coders,
The event OnClick on the shape works great.

Is there any way to assign a new event "OnDblClick" to the shape at runtime?
How can I do it?


Code: Pascal  [Select][+][-]
  1. var
  2.   aShape:Tshape;
  3. begin
  4.   aShape:=TShape.Create(nil);
  5.   with aShape do
  6.   begin
  7.     left:=3;
  8.     top:=3;
  9.     parent:=Form1;
  10.     width:=100;
  11.     height:=100;
  12.     shape:=stRectangle;
  13.     OnClick:= @TestClick;
  14.     OnDblClick:= @TestDoubleClick;
  15.   end;
  16. end;

OnDblClick is not registered event:
Error: identifier idents no member "OnDblClick".

Many thanks in advance.
« Last Edit: April 13, 2020, 02:19:14 am by AsleyCruz »
AsleyCruz - Graphic and apps designer
-
http://www.asleycruz.com/

lainz

  • Hero Member
  • *****
  • Posts: 3637
    • Lainz
Re: Assigning double click to shape at runtime?
« Reply #1 on: April 13, 2020, 01:24:38 am »
You need to create a class that inherits from TShape and publish the property, since OnDblClick is not published for TShape.
https://lainz.github.io - My Website
https://lazpaint.github.io/ - LazPaint Downloads

ASerge

  • Hero Member
  • *****
  • Posts: 1636
Re: Assigning double click to shape at runtime?
« Reply #2 on: April 13, 2020, 01:57:13 am »
The event OnClick on the shape works great.
Is there any way to assign a new event "OnDblClick" to the shape at runtime?
Both OnClick and OnDblClick are not published by TShape, but OnClick is public by default, but OnDblClick is protected.
Use class helpers to get protected properties:
Code: Pascal  [Select][+][-]
  1. type
  2.   TSetOnDbkClickHelper = class helper for TControl
  3.     procedure SetDblClick(DblClickEvent: TNotifyEvent); inline;
  4.   end;
  5.  
  6. procedure TSetOnDbkClickHelper.SetDblClick(DblClickEvent: TNotifyEvent);
  7. begin
  8.   OnDblClick := DblClickEvent;
  9. end;

Test with TShape on Form OK:
Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.     Shape1: TShape;
  4.     procedure FormCreate(Sender: TObject);
  5.   private
  6.     procedure ShapeDblClick(Sender: TObject);
  7.   end;
  8.  
  9. //...
  10.  
  11. procedure TForm1.FormCreate(Sender: TObject);
  12. begin
  13. //  Shape1.OnDblClick := nil;
  14.   Shape1.SetDblClick(@ShapeDblClick);
  15. end;
  16.  
  17. procedure TForm1.ShapeDblClick(Sender: TObject);
  18. begin
  19.   Caption := 'Double click time ' + TimeToStr(Time);
  20. end;

AsleyCruz

  • Jr. Member
  • **
  • Posts: 78
    • Graphic and apps designer
Re: Assigning double click to shape at runtime?
« Reply #3 on: April 13, 2020, 02:19:01 am »
Both OnClick and OnDblClick are not published by TShape, but OnClick is public by default, but OnDblClick is protected.
Use class helpers to get protected properties:

Thanks ASerge, problem was solved and worked like a charm.
Many thanks.
AsleyCruz - Graphic and apps designer
-
http://www.asleycruz.com/

 

TinyPortal © 2005-2018