Recent

Author Topic: [SOLVED] Setter for a component  (Read 7289 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[SOLVED] Setter for a component
« on: December 14, 2021, 07:36:05 pm »
I have a component where I can assign another component as a property (see below). The problem is when I drop my component and say a button on a form, then assign the button to the source property, at first all is OK. If I then delete the button on the form the IDE crashes.
How can I fix that?

Code: Pascal  [Select][+][-]
  1. unit MyZoomX;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls, ComCtrls;
  10.  
  11. type
  12.  
  13.   { TMyZoomX }
  14.  
  15.   TMyZoomX = class(TComponent)
  16.   private
  17.     fSource : TControl;
  18.     procedure SetSource(aValue : TControl);
  19.     function  GetSource : TControl;
  20.   protected
  21.   public
  22.     constructor Create(AOwner : TComponent); override;
  23.   published
  24.     property Source : TControl read GetSource write SetSource;
  25.   end;
  26.  
  27. procedure Register;
  28.  
  29. implementation
  30.  
  31. constructor TMyZoomX.Create(AOwner : TComponent);
  32. begin
  33.   inherited Create(AOwner);
  34. end;
  35.  
  36. procedure TMyZoomX.SetSource(aValue : TControl);
  37. begin
  38.   fSource := aValue;
  39. end;
  40.  
  41. function TMyZoomX.GetSource : TControl;
  42. begin
  43.   Result := fSource;
  44. end;
  45.  
  46. procedure Register;
  47.  
  48. begin
  49.   RegisterComponents('Misc',[TMyZoomX]);
  50. end;
  51.  
  52. end.
  53.  
« Last Edit: December 16, 2021, 09:09:01 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: [CLOSED] Setter for a component
« Reply #1 on: December 15, 2021, 03:26:17 am »
I originally tried

property Source : TControl read fSource write fSource;
« Last Edit: December 15, 2021, 08:33:49 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Sieben

  • Sr. Member
  • ****
  • Posts: 374
Re: Setter for a component
« Reply #2 on: December 16, 2021, 12:21:53 am »
Same here with 2.0.10 - but isn't it a matter with TColorButton not overriding Notification...?
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

wp

  • Hero Member
  • *****
  • Posts: 12864
Re: Setter for a component
« Reply #3 on: December 16, 2021, 12:25:08 am »
now delete the Color Dialog and watch the crash..
This is because there is no Notification method in TColorButton. Add this to colorbutton.inc, and the crash will be gone (committed to Laz/main):
Code: Pascal  [Select][+][-]
  1. procedure TColorButton.Notification(AComponent: TComponent; Operation: TOperation);
  2. begin
  3.   inherited;
  4.   if (AComponent = FColorDialog) and (Operation = opRemove) then
  5.     FColorDialog := nil;
  6. end;

I doubt that it did work in Laz 2.0.6 because in 2.0.8 (I don't have 2.0.6 any more) the Notification method is missing, too.
« Last Edit: December 16, 2021, 12:41:53 am by wp »

wp

  • Hero Member
  • *****
  • Posts: 12864
Re: Setter for a component
« Reply #4 on: December 16, 2021, 12:44:34 am »
I already fixed it for TColorButton - see my previous post. Every control having other controls as properties must override the Notification method. The control to be deleted calls the Notification method and the owing control can set its reference to zero.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: [SOLVED] Setter for a component
« Reply #5 on: December 16, 2021, 09:10:58 am »
@wp - Thanks. Did the trick.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 12864
Re: [SOLVED] Setter for a component
« Reply #6 on: December 16, 2021, 01:44:02 pm »
jamie, please let's continue this discussion in the bugtracker.

 

TinyPortal © 2005-2018