2012-09-19 11 views
4

필자는 작성한 구성 요소를 기본 폼에 패널로 전달합니다. 여기구성 요소가 해제되었음을 어떻게 감지합니까?

은 매우 단순화 된 예입니다

procedure TMy_Socket.StatusPanel_Add(AStatusPanel: TPanel); 

필요에 따라이 구성 요소는 다음 패널 캡션을 업데이트합니다.

내 주 프로그램에서 다음 번에 구성 요소가 패널을 업데이트하려고하면 FreeAndNil 패널에 AV가 표시됩니다. 이유에 대해 이해합니다. 이제 구성 요소의 패널 참조가 정의되지 않은 위치를 가리키고 있습니다.

패널이 해제되어 참조 할 수 없다는 것을 알면 구성 요소 내에서 어떻게 감지 할 수 있습니까?

나는 if (AStatusPanel = nil)을 시도했으나 nil이 아니며 여전히 주소가 있습니다.

답변

6

당신은 패널의 FreeNotification() 메소드를 호출 한 다음 TMy_Socket 구성 요소가 가상 Notification() 메소드를 오버라이드 (override)이 있어야, 예를 들어, (당신의 명명 체계 주어, 나는 당신이 당신의 구성 요소에 여러에게 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; 
3

다른 구성 요소가 해제 될 때 구성 요소에 통지해야하는 경우 TComponent.FreeNotification을보십시오. 그것은 단지 당신이 필요로해야합니다.

+0

@ 스티브 : 당신은 당신의'TMy_Socket' 클래스에 알림 PROC을 넣어. 문서 페이지 하단의 예를 확인하십시오. –

관련 문제