We help IT Professionals succeed at work.

Deleting owned components.

MoonCalf
MoonCalf asked
on
Hi.

I've created a set of components that all had a set of properties that were standard throughout.  So, I took these properties and made a component to hold these, and simply made the other components require a reference to this new component type.

EG.  Components T1, T2 & T3 need a username and password.
         I created Component T4 that holds a username and password.
         T1, T2 & T3 now have a 4 property, requiring a T4.

I drop a T1 on the form, then drop a T4 on the form, assign the T4 to the 4 property of the T1.

Great!  It all works fine, but I've found a problem.

If I delete the T4 component from the form, after assigning it to a T1, T2 or T3, I get error messages galore!

How can I handle this?

MoonCalf
Comment
Watch Question

Igor UL7AAjrSenior developer

Commented:
Hi MoonCalf,

There is Notification method that should be overriden.

TYourComponent = class(...)  
   procedure Notification(AComponent: TComponent; Operation: TOperation); override;
end;

procedure TYourComponent.Notification(AComponent: TComponent; Operation: TOperation);
begin
  if (AComponent = ReferenceProperty) and
    (AComponent <> nil) and
    (Operation = opRemove) then
    ReferenceProperty := nil;
end;

Author

Commented:
Which component do I put this in Igor?
Senior developer
Commented:
Hi MoonCalf.

Spent all your points? Forget it, doesn't matter:-)


You can put it in component that has property as reference to another component.
Here is more detailed sample:

type
  TT4 = class(...)
  private
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  published
    property T1: TT1...    
    property T2: TT2...    
    property T3: TT3...    
  end;

....

procedure TT4.Notification(AComponent: TComponent; Operation: TOperation);
begin
  if (AComponent = T1) and
    (AComponent <> nil) and
    (Operation = opRemove) then
    T1 := nil;
  ...
  if (AComponent = T3) and
    (AComponent <> nil) and
    (Operation = opRemove) then
    T3 := nil;
end;

----
Igor

Author

Commented:
Sorry Igor, but I'm still having problems.

Does it make a difference that the component is based on a TStringGrid, which has it's own Notification event?

MoonCalf.

Author

Commented:
I got it working!!!

Thanks a lot for your help Igor.  Sorry about the lack of points.  I'll chuck some more your way when I'm a bit more endowed : )

MoonCalf
Igor UL7AAjrSenior developer

Commented:
Hi MoonCalf,

>>Sorry about the lack of points.  
Don't worry, next time, all OK.

Glad to help you.

----
Igor.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.