2008-12-17 18 views
6

我寫了一個自定義控件,它有幾個子面板。我希望這些子面板在設計時接受任何額外的控制。如何使用在設計時接受其他控件的子面板創建自定義控件?

不幸的是,任何在設計時被丟棄的控件都會在我的自定義控件上而不是在面板上。這特別表明,如果我嘗試刪除標籤:顯示標籤的藍色圓點,但它的標題不是,如果我取消選擇標籤,它將不再可見。

簡化的代碼(只有一個子面板):

type 
    TMyContainer = class(TPanel) 
    p_SubPanel: TPanel; 
    public 
    constructor Create(_Owner: TComponent); override; 
    end; 

    constructor TMyContainer.Create(_Owner: TComponent); 
    begin 
    inherited; 
    p_SubPanel := TPanel.Create(Self); 
    p_SubPanel.Parent := Self; 
    p_SubPanel.Align := alClient; 
    end; 

我在做什麼錯在這裏?

(以防萬一它的問題:我使用德爾福2007年)

[編輯]

我現在已經解決了不同的看法。該組件不再包含面板,而是指外部面板。這使得它實際上更加靈活,但在不利方面,它不再像使用那樣直觀。

我仍然想知道如何完成我最初描述的內容。沒有一個開源組件可以做到這一點,所以我可以研究源代碼?

回答

1

我不知道細節,但你是否將標籤的父母設置爲子面板?如果在設計時您可能需要在您的主要組件(例如您的面板所在的容器)中編寫代碼,找出哪個子面板正在接受組件,並將標籤父屬性設置爲該子面板。

我敢肯定,當一個組件被添加或從另一個組件中移除時,通知方法被調用,這應該有助於追蹤你需要放置代碼的位置。

2

這是一個很好的問題。您可以允許您的自定義TWinControl在設計時通過向控件ControlStyle屬性添加csAcceptControls來讓其他控件在其上放置。

constructor TMyContainer.Create(AOwner: TComponent); 
begin 
    inherited; 
    ControlStyle := ControlStyle + [csAcceptControls]; 
end; 

但在努力工作,這一點,我已經受夠了能夠自定義的控制範圍內控件拖放到子板收效甚微。將csAcceptControls添加到子面板的ControlStyle是不夠的。我已經得到的cloest是說服子板它被設計像這樣一個黑客:

type 
    TGiveMeProtected_Component = class(TComponent); 

procedure TMyContainer.Create(AOwner: TComponent); 
begin 
    FSubPanel := TPanel.Create(Self); 
    TGiveMeProtected_Component(FSubPanel).SetDesigning(True, True); 
end; 

使用代碼,您現在就可以控件拖放到子面板,但它意味着你也可以選擇子面板,改變它的屬性,甚至刪除它,你肯定不想要的。對不起,我不能拿出答案,我仍然很想知道你是否解決了這個問題。 :)

0

我這樣做了,但最終取代了常規面板的控件,當需要時會顯示/隱藏。

而不是從TPanel降序我的控件從TCustomControl下降。我不認爲我能從TPanel下行,但不記得問題是什麼。

的容器控制:

TPageControl = class(TCustomControl) 
private 
    PageList:TObjectList; // To hold references to all sub-pages for easy access. 
end; 

constructor TPageControl.Create(AOwner: TComponent); 
begin 
    inherited; 
    ControlStyle := ControlStyle + [csAcceptsControls]; 
    PageList := TObjectList.Create; 
    PageList.OwnsObjects := false; 
end; 

destructor TVstPageControl.Destroy; 
begin 
    PageList.Free; 
    inherited; 
end; 

procedure TPageControl.NewPage; 
var 
    Page:TPage; 
begin 
    Page := TPage.Create(Self.Owner); 
    Page.Parent := Self; 
    Page.Align := alClient; 

    PageList.Add(Page); 
end; 

procedure TPageControl.DeletePage(Index:integer); 
var 
    Page:TPage; 
begin 
    Page := PageList[Index] as TPage; 
    Page.Free; 
    PageList.Delete(Index); 
end; 

頁面/子面板控制:

TVstPage = class(TCustomControl) 
public 
    constructor Create(AOwner: TComponent); override; 
end; 

constructor TPage.Create(AOwner: TComponent); 
begin 
    inherited; 
    ControlStyle := ControlStyle + [csAcceptsControls]; 
end; 
相關問題