Recent

Author Topic: Executing owner methods  (Read 585 times)

Gald

  • New Member
  • *
  • Posts: 47
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?

speter

  • Full Member
  • ***
  • Posts: 210
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. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

Gald

  • New Member
  • *
  • Posts: 47
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 »

lucamar

  • Hero Member
  • *****
  • Posts: 4009
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

  • New Member
  • *
  • Posts: 47
Re: Executing owner methods
« Reply #4 on: April 16, 2021, 10:18:09 pm »
Very good, Lucamar!
I'll use it a lot.

 

TinyPortal © 2005-2018