我有两个名为 mdfi
和 form1
的表单。我想通过 form1
中的代码使 mdfi
形成 MdiContainer
。我尝试了以下操作,但程序在运行时关闭:
private void Form1_Deactivate(object sender, EventArgs e)
{
this.TopMost = false;
Mdfi newMDIChild = new Mdfi();
newMDIChild.IsMdiContainer = true;
this.MdiParent = newMDIChild;
newMDIChild.Show();
}
最佳答案
将应用程序的主窗口更改为子窗口会产生许多副作用。由于 MdiParent 分配,Winforms 被迫销毁窗口。这足以完成 Main() 方法中的 Application.Run() 调用,这就是应用程序的结束。您必须更改它:
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var main = new Form1();
main.Show();
Application.Run();
}
并且您必须确保现在在 MDI 父级关闭时终止:
private void Form1_Deactivate(object sender, EventArgs e) {
this.TopMost = false;
var newMdiParent = new mdfi();
newMdiParent.IsMdiContainer = true;
this.MdiParent = newMdiParent;
newMdiParent.FormClosed += (s, ea) => Application.Exit();
newMdiParent.Show();
this.Deactivate -= Form1_Deactivate;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
if (this.MdiParent == null) Application.Exit();
}
关于c# - MdiContainer 表单未打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21628622/