【问题标题】:Move controls from the user control to a panel control将控件从用户控件移动到面板控件
【发布时间】:2012-08-25 19:46:34
【问题描述】:

我正在尝试编写一个表单主题类库,以便以一种简单的方式为我将从事的任何项目调整表单布局。
这基本上是它应该是什么样子的一个想法:

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,这在普通表单中是不可能的。 所以我想我可以通过自动将控件添加到内容面板而不是用户控件来解决这个问题。

所以我试图做的是:

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

所以我的问题是:有没有办法解决这个错误,并将控件从用户控件移动到内容面板?
请注意,我确实希望在 ThemedForm 类中自动执行此操作,而不是从主窗体调用方法。

编辑:
我试过这个:
http://forums.asp.net/t/617980.aspx
但这只会导致visual studio死机,然后我需要重新启动。

【问题讨论】:

  • 哪一行出现错误?
  • 它并没有真正发生在某一行,它发生在我尝试在可视化编辑器以及 FormToTheme 类中添加控件时。但我的猜测是,错误是由我尝试将控件从当前父级移动到 contentPanel 的代码行引起的。 (注意错误不会在错误日志中弹出,但会导致MessageBox显示错误)
  • 我的猜测是设计师特别奇怪。如果从代码中手动添加控件会怎样?
  • 完全相同的东西。这确实是一个问题,因为 ThemedForm 类不拥有我要添加的控件,而是 FormToTheme 类拥有它们。
  • 我对此表示怀疑。您的 FormToTheme 类不包含 ThemedForm,它是一个 ThemedForm...

标签: c# user-controls controls panel


【解决方案1】:

我知道回答自己的问题并不合适,但是我想出的解决方案需要相当多的解释,这对于通过编辑添加到我的问题中来说太多了。

所以我们开始:

在继承的 '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 属性,它会停靠在内容面板内,而不是停靠在整个表单上。

这仍然有点问题,因为在可视化编辑器中,停靠的控件似乎仍然停靠在整个表单上,但是在运行应用程序时,结果是我想要的。

我真的希望这对任何人都有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 2011-08-01
    • 2010-10-11
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多