c# - MdiContainer 表单未打开

标签 c# winforms c#-2.0 mdi mdichild

我有两个名为 mdfiform1 的表单。我想通过 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/

相关文章:

c# - 如何锁定和解锁 SQL SERVER 表?

c# - 反射:调用方法,传递委托(delegate)作为参数

c# - 在C#中如果 "using system"被注释掉,关键字从何而来?

c# - 异常没有被捕获 block

winforms - 我应该使用什么 GUI 工具包

c# - WPF DataGrid - 分级色标上的颜色单元格

c# - RichTextBox C# 设置插入符位置 winforms

c#-2.0 - 对字符串类型的数据表列求和

c# - 如何暂停 DataTable 通知或数据绑定(bind)以防止 UI 更新