1.MDI 单实例
Code
private readonly Dictionary<Type, Form> _singleInstanceForms = new Dictionary<Type, Form>();
protected Form ActivateForm<T>() where T : Form, new()
{
if (!this._singleInstanceForms.ContainsKey(typeof(T)))
{
T newForm = new T();
// Set up the necessary properties
newForm.MdiParent = this;
newForm.FormClosed += delegate(object sender, FormClosedEventArgs e)
{
_singleInstanceForms.Remove(sender.GetType());
};
_singleInstanceForms.Add(typeof(T), newForm);
}
Form formToActivate = this._singleInstanceForms[typeof(T)];
formToActivate.Show();
formToActivate.TopLevel = false;
formToActivate.Activate();
return formToActivate;
}
2.无法将顶级控件添加到控件
myForm.TopLevel = false;//设置Form的值,由于Form默认为顶级控件