Recent

Author Topic: Executing owner methods  (Read 1536 times)

Gald

  • Full Member
  • ***
  • Posts: 107
Executing owner methods
« on: April 16, 2021, 11:46:47 am »
Hi!
I'm getting trouble trying to execute a method of another component.

Small sample attached! It's self explained

Code: Pascal  [Select][+][-]
  1. // The method
  2. procedure TForm1.Panel1DblClick(Sender: TObject);
  3. begin
  4.   TPanel(Sender).Color:=clGreen;
  5. end;
  6.  
  7. // The action to simulate it
  8. procedure TForm1.ShapeLeft1MouseDown(Sender: TObject; Button: TMouseButton;
  9.   Shift: TShiftState; X, Y: Integer);
  10. var
  11. C : TComponent;
  12. begin
  13.   C := TShape(Sender).GetParentComponent;
  14.   TPanel(C).OnDblClick:=(nil);
  15. end;  
  16.  

If the code above is executed (in runtime), the first one is just ignored, with no error.

What am I doing wrong?
Lazarus 2.0.12 r64642 FPC 3.2.0 x86_64-win64-win32/win64/Manjaro KDE 21
AMD Ryzen 3 1300X Quad-Core Processor 3.50 GHz / 8,00 GB RAM / GTX 1500 TI / 2TB M.2 NVMe

speter

  • Sr. Member
  • ****
  • Posts: 349
Re: Executing owner methods
« Reply #1 on: April 16, 2021, 12:06:22 pm »
Am I missing something?

(a) If the user double-clicks on the panel before clicking on the shape, I would expect the panel's colour to change.
(b) If the user clicks on the shape then (I would expect) the panel's double click event to no longer work.

Are you saying that case (a) is not happening?

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Gald

  • Full Member
  • ***
  • Posts: 107
Re: Executing owner methods
« Reply #2 on: April 16, 2021, 12:21:02 pm »
Are you saying that case (a) is not happening?

Oh no, sorry!
Two days without sleep.  :-[

I get it.


Changed last line to:
TPanel(C).Color:=clGreen;
« Last Edit: April 16, 2021, 12:24:36 pm by Gald »
Lazarus 2.0.12 r64642 FPC 3.2.0 x86_64-win64-win32/win64/Manjaro KDE 21
AMD Ryzen 3 1300X Quad-Core Processor 3.50 GHz / 8,00 GB RAM / GTX 1500 TI / 2TB M.2 NVMe

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Executing owner methods
« Reply #3 on: April 16, 2021, 06:36:49 pm »
To execute a method, call it a as if it were any other procedure/function (which, albeit somewhat special, it *is*):

Code: Pascal  [Select][+][-]
  1. // The action to simulate it
  2. procedure TForm1.ShapeLeft1MouseDown(Sender: TObject; Button: TMouseButton;
  3.   Shift: TShiftState; X, Y: Integer);
  4. var
  5. C : TComponent;
  6. begin
  7.   C := TShape(Sender).GetParentComponent;
  8.   { In case you move the shape out of the panel
  9.     or discard its OnDblClick tomorrow ... or next year ...}
  10.   if (C is TPanel) and Assigned(TPanel(C).OnDblClick) then
  11.     TPanel(C).OnDblClick(nil); {Just call it!}
  12. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Gald

  • Full Member
  • ***
  • Posts: 107
Re: Executing owner methods
« Reply #4 on: April 16, 2021, 10:18:09 pm »
Very good, Lucamar!
I'll use it a lot.
Lazarus 2.0.12 r64642 FPC 3.2.0 x86_64-win64-win32/win64/Manjaro KDE 21
AMD Ryzen 3 1300X Quad-Core Processor 3.50 GHz / 8,00 GB RAM / GTX 1500 TI / 2TB M.2 NVMe

 

TinyPortal © 2005-2018