我有这些课程:
internal partial class FBase : Form
{
public FBase() { InitializeComponent(); }
public FBase(bool owner) : this()
{
if (!owner) { this.Opacity = 0; Load += (s, e) => Close(); }
}
}
internal partial class Form1 : FBase
{
public Form1(bool owner) : base(owner) { InitializeComponent(); }
}
此代码有效(不显示 Form1):
Form1 f = new Form1(false);
if(f != null) { f.MdiParent = parent; f.Show(); }
但这不起作用(显示 Form1):
OpenSingleMdiChild(() => new Form1(false));
这是 OpenSingleMdiChild 的实现:
public static void OpenSingleMdiChild<T>(this Form parent, Func<T> factory) where T : Form
{
T f = null;
foreach (Form c in parent.MdiChildren) if (c.GetType() == typeof(T)) { f = c; break; }
if (f == null) { f = factory(); f.MdiParent = parent; }
f.Show(); f.Select();
}