2014-02-07 77 views
2

我有两种形式,名为mdfiform1。我想通过form1的代码使mdfi形成MdiContainer。我尝试了以下但运行时关闭的程序:Mdi容器窗体不能打开

private void Form1_Deactivate(object sender, EventArgs e) 
{ 
    this.TopMost = false; 
    Mdfi newMDIChild = new Mdfi(); 
    newMDIChild.IsMdiContainer = true; 
    this.MdiParent = newMDIChild; 
    newMDIChild.Show(); 
} 
+1

你为什么想停用事件过程中添加的形式()! – User999999

+0

我正在制作一个图片查看器,看起来像Google Picasa,所以当用户点击屏幕上的任何位置时,它会制作一个mdi表单并且图像适合该图像。 –

+0

MDI表单位于父表单内。我不确定这是你想要的。当这个代码被调用时,你正在按[X]按钮关闭form1吗? –

回答

2

将您的应用程序的主窗口更改为子窗口有许多副作用。由于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(); 
    } 
相关问题