【发布时间】:2012-08-25 19:46:34
【问题描述】:
我正在尝试编写一个表单主题类库,以便以一种简单的方式为我将从事的任何项目调整表单布局。
这基本上是它应该是什么样子的一个想法:
本质上,插件的工作原理如下:
// 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