我正在做一些定制
Control
类,需要对它们进行一些初始化,这取决于它们被添加到表单中。发生这种情况时是否会发生火灾?
我认为这个样本应该足以显示我正在尝试做的事情:
public interface IMyForm
{
ISomeObject SomeObject {get; set; }
}
class MyForm : IMyForm
{
//eg InitializeComponent() as well as several others called at later points
private MethodThatAddsAControl()
{
MyControl newControl = new MyControl();
//other initialization as needed
//does this raise an event in MyControl I can use to call
//InitializationAfterBeingAddedToForm()?
this.Controls.Add(newControl);
}
}
class MyControl : Control
{
InitializationAfterBeingAddedToForm()
{
//can't be done in the constructor because at that point FindForm() will return null
(FindForm() as IMyForm).SomeObject.AnEvent += new EventHandler(SomeObject_AnEvent);
}
}
事实证明,这比我最初意识到的要困难得多,我想我必须把博卢和迈克·多尔的建议结合起来。问题是当一些
MyControl
s直接添加到形式中,在这种情况下,bolu的解决方案工作得很好。其他的则添加到面板中,而不是直接添加到表单中。我想我已经拼凑出了一个涉及Bolu对前一个案例的解决方案,并做了一些修改,以处理由添加的小组引发的事件,而不是
我的控制
在里面,米克负责处理
我的控制
在构造函数完成运行后将添加到面板中。明天早上我得再做一次测试,然后才能确信它能起作用。
当我尝试在设计器中使用他的建议时,Bolu请求的错误消息:
Failed to create component 'MyControl'. The error message follows:
'System.MissingMethodException: Constructor on type 'MyNamespace.MyControl' not found.
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.ComponentModel.Design.DesignSurface.CreateInstance(Type type)
at Microsoft.VisualStudio.Design.VSDesignSurface.CreateInstance(Type type)
at System.ComponentModel.Design.DesignerHost.System.ComponentModel.Design.IDesignerHost.CreateComponent(Type componentType, String name)
at System.ComponentModel.Design.DesignerHost.System.ComponentModel.Design.IDesignerHost.CreateComponent(Type componentType)
at System.Drawing.Design.ToolboxItem.CreateComponentsCore(IDesignerHost host)
at System.Drawing.Design.ToolboxItem.CreateComponentsCore(IDesignerHost host, IDictionary defaultValues)
...'
当我得到错误时,构造器就就位了。
public MyControl(Form parent)
{
_parent = parent as IMyForm;
parent.ControlAdded += new ControlEventHandler(parent_ControlAdded);
Initialize(); //does rest of initialization
}
public TimelineControl(Form parent, Panel container)
{
_parent = parent as IMyForm;
container.ControlAdded += new ControlEventHandler(parent_ControlAdded);
Initialize(); //does rest of initialization
}