C# MDI容器窗体未打开
我有两个表单,分别是C# MDI容器窗体未打开,c#,winforms,c#-2.0,mdi,mdichild,C#,Winforms,C# 2.0,Mdi,Mdichild,我有两个表单,分别是mdfi和form1。我想通过form1中的代码制作mdfi表单aMdiContainer。我尝试了以下操作,但运行时程序关闭: private void Form1_Deactivate(object sender, EventArgs e) { this.TopMost = false; Mdfi newMDIChild = new Mdfi(); newMDIChild.IsMdiContainer = true; this.MdiPar
mdfi
和form1
。我想通过form1
中的代码制作mdfi
表单aMdiContainer
。我尝试了以下操作,但运行时程序关闭:
private void Form1_Deactivate(object sender, EventArgs e)
{
this.TopMost = false;
Mdfi newMDIChild = new Mdfi();
newMDIChild.IsMdiContainer = true;
this.MdiParent = newMDIChild;
newMDIChild.Show();
}
将应用程序的主窗口更改为子窗口有许多副作用。Winforms由于MdiParent分配而被迫销毁窗口。这足以让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();
}
为什么在Deactivate事件()期间尝试添加表单!我正在使图片查看器看起来像Google picasa,因此,当用户在屏幕上单击任何地方时,它会生成一个mdi表单,并且图像适合该表单。mdi表单位于父表单中。我不确定这是否是你想要的。当调用此代码时,您是否按[X]按钮关闭
form1
?@JeffBridgman当我的form1停用时,我想将我的“mdfi”表单设置为mdi父级。