簡體   English   中英

將控件從用戶控件移動到面板控件

[英]Move controls from the user control to a panel control

我正在嘗試編寫一個表單主題類庫,以便以我將要處理的任何項目的簡單方式調整表單布局。
這基本上是一個它應該是什么樣子的想法:

http://www.beaverdistrict.nl/form_layout.png

從本質上講,該插件的工作原理如下:

 // form class, which inherits the plugin class class FormToTheme : ThemedForm { public FormToTheme() { // some code here } } // plugin class itself class ThemedForm: Form { public ThemedForm() { // some code here } } 

基本上我將FormBorderStyle設置為None,並按代碼繪制布局。
但是現在,添加的控件可以放在自定義標題欄上,如果保留默認的FormBorderStyle,這在正常形式下是不可能的。 所以我認為我可以通過自動將控件添加到內容面板而不是usercontrol來解決這個問題。

所以我試圖做的是:

 private void ThemedForm_ControlAdded(Object sender, ControlEventArgs e) { // some simple code to set the control to the current theme I'm using e.Control.BackColor = Color.FromArgb(66, 66, 66); e.Control.ForeColor = Color.White; // the code where I try to place the control in the contentPanel controls array, // and remove it from it's parent's controls array. if (e.Control.Name != contentPanel.Name) { e.Control.Parent.Controls.Remove(e.Control); contentPanel.Controls.Add(e.Control); } } 

但是當我嘗試在主窗體和可視化編輯器中添加新控件時,我收到以下錯誤:

 child is not a child control of this parent 

所以我的問題是:有沒有辦法解決這個錯誤,並將控件從usercontrol移動到內容面板?
請注意,我確實希望在ThemedForm類中自動執行此操作,而不是從主窗體中調用方法。

編輯:
我試過這個:
http://forums.asp.net/t/617980.aspx
但這只會導致visual studio凍結,然后我需要重啟。

我知道回答自己的問題並不合適,但是我提出的解決方案會有相當多的解釋,在編輯中加入我的問題太多了。

所以我們走了:

在繼承的'ThemedForm'類中,我創建了一個私有變量,以便能夠在調用Controls屬性時返回變量:

private Controls controls = null;

我將變量設置為null,因為我需要將變量傳遞給'ThemedForm'類構造函數中的類。 我稍后將創建該類的新實例。

然后我創建了一個類來替換Controls屬性:

public class Controls
{
    private Control contentPanel = null;
    private ThemedForm themedform = null;
    public Controls(ThemedForm form, Control panel)
    {
        contentPanel = panel;
        themedform = form;
    }
    public void Add(Control control)
    {
        if (control != contentPanel)
        {
            contentPanel.Controls.Add(control);
        }
        else
        {
            themedform.Controls_Add(control);
        }
    }
    public void Remove(Control control)
    {
        if (control != contentPanel)
        {
            contentPanel.Controls.Remove(control);
        }
        else
        {
            themedform.Controls_Remove(control);
        }
    }
}

我知道這個類遠離原始Controls屬性的所有功能,但是現在必須這樣做,如果你願意,你可以添加自己的功能。

正如您在Controls類的Add和Remove方法中所看到的,我嘗試確定需要添加的控件是否是我想要添加其余控件的內容面板,或者需要的任何其他控件被添加到內容面板。

如果控件實際上是內容面板,我在'ThemedForm'類的基類的Controls屬性中添加或刪除它,這是一個'Form'類。 否則,我只是將控件添加到內容面板的Controls屬性。

然后我將Controls_Add和Controls_Remove方法添加到'ThemedForm'類中,以便能夠從'ThemedForm'基類的Controls屬性添加或刪除控件。

public void Controls_Add(Control control)
{
    base.Controls.Add(control);
}
public void Controls_Remove(Control control)
{
    base.Controls.Remove(control);
}

它們非常不言自明。

為了從外部類調用Controls.Add或Controls.Remove方法,我需要添加一個隱藏當前Controls屬性的公共屬性,並返回我分配給替換類的私有變量。

new public Controls Controls
{
    get { return controls; }
}

最后,我創建了一個Controls類的新實例,傳遞了當前的'ThemedForm'類和contentPanel控件,以便讓它全部運行。

_controls = new Controls(this, contentPanel);

完成所有這些后,我能夠將添加到UserControl的任何控件(甚至在可視化編輯器中)“重定向”到內容面板。 這允許我使用任何控件的Dock屬性,它將停靠在內容面板內,而不是整個表單。

這仍然是一個小錯誤,因為在可視化編輯器中,停靠的控件似乎仍然停靠在整個表單上,但在運行應用程序時,結果是我想要的。

我真的希望這可以幫助任何人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM