您必须调用面板的FreeNotification()
方法,然后让您的TMy_Socket
组件覆盖虚拟Notification()
方法,例如(根据您的命名方案,我假设您可以TPanel
向组件添加多个控件):
type
TMy_Socket = class(TWhatever)
...
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
...
public
procedure StatusPanel_Add(AStatusPanel: TPanel);
procedure StatusPanel_Remove(AStatusPanel: TPanel);
...
end;
procedure TMy_Socket.StatusPanel_Add(AStatusPanel: TPanel);
begin
// store AStatusPanel as needed...
AStatusPanel.FreeNotification(Self);
end;
procedure TMy_Socket.StatusPanel_Remove(AStatusPanel: TPanel);
begin
// remove AStatusPanel as needed...
AStatusPanel.RemoveFreeNotification(Self);
end;
procedure TMy_Socket.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if (AComponent is TPanel) and (Operation = opRemove) then
begin
// remove TPanel(AComponent) as needed...
end;
end;
如果您一次只跟踪一个TPanel
:
type
TMy_Socket = class(TWhatever)
...
protected
FStatusPanel: TPanel;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
...
public
procedure StatusPanel_Add(AStatusPanel: TPanel);
...
end;
procedure TMy_Socket.StatusPanel_Add(AStatusPanel: TPanel);
begin
if (AStatusPanel <> nil) and (FStatusPanel <> AStatusPanel) then
begin
if FStatusPanel <> nil then FStatusPanel.RemoveFreeNotification(Self);
FStatusPanel := AStatusPanel;
FStatusPanel.FreeNotification(Self);
end;
end;
procedure TMy_Socket.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if (AComponent = FStatusPanel) and (Operation = opRemove) then
FStatusPanel := nil;
end;