Lazarus

Free Pascal => Beginners => Topic started by: Gald on April 16, 2021, 11:46:47 am

Title: Executing owner methods
Post by: Gald 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?
Title: Re: Executing owner methods
Post by: speter 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.
Title: Re: Executing owner methods
Post by: Gald 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;
Title: Re: Executing owner methods
Post by: lucamar 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;
Title: Re: Executing owner methods
Post by: Gald on April 16, 2021, 10:18:09 pm
Very good, Lucamar!
I'll use it a lot.
TinyPortal © 2005-2018