简体   繁体   English

当我关闭最大化的MDI子窗体时,什么也没有发生

[英]Nothing happens when I close maximized MDI child form

I have Form1 with 2 radio buttons ( rb1 and rb2 ) and one ordinary button ( btn ). 我的Form1带有2个单选按钮( rb1rb2 )和一个普通按钮( btn )。 When I click on btn I should open Form2 , as MDI child of Form1 if rb1 is checked, or as ordinary Dialog if rb2 is checked. 当我点击btn我应该打开Form2 ,如MDI子Form1 ,如果rb1被选中,还是作为普通的Dialog ,如果rb2被选中。 Also, there can only be one Form2 opened at any moment. 此外,任何时候都只能打开一个Form2

This is my code: 这是我的代码:

public partial class Form1 : Form
{

    Form2 f2;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (f2 != null) 
        {
            MessageBox.Show("Close form!");
            return;
        }

        f2 = new Form2();
        if (radioButton1.Checked == true)
        {
            this.IsMdiContainer = true;
            f2.MdiParent = this;
            f2.Show();
        }
        else
        {
            f2.Show();
        }

        f2.FormClosed += f2_FormClosed;

    }

    void f2_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.IsMdiContainer = false;
        f2 = null;
    }

}

Everything works as it should except when I maximize Form2 as MDI child and then close it. 一切正常,除了将Form2最大化为MDI子级然后将其关闭时。 After that screen stays the same (as I even didn't closed Form2 ) but I am able to open new Form2 , and then Form1 's title is " Form1 - [Form2] ", and if I repeat the process it would be " Form1 - [Form2] - [Form2] ", etc. 在该屏幕保持不变之后(即使我没有关闭Form2 ),但我能够打开新的Form2 ,然后Form1的标题为“ Form1 - [Form2] ”,如果我重复该过程,它将为“ Form1 - [Form2] - [Form2]

I figured out that I my f2_FormClosed method should be 我发现我的f2_FormClosed方法应该是

    void f2_FormClosed(object sender, FormClosedEventArgs e)
    {
        f2.Hide(); // <<<<<<<<-----------NEW
        this.IsMdiContainer = false;
        f2 = null;
    }

but I don't know why; 但我不知道为什么; Form2 should be closed, I don't know why should I have to hide it?! Form2应该关闭,我不知道为什么我必须隐藏它?

Thanks! 谢谢!

I agree with Hans, switching IsMdiContainer at run-time is wonky and is likely to produce other side-effects you haven't seen yet. 我同意Hans的观点,在运行时切换IsMdiContainer很奇怪,并且可能会产生您尚未看到的其他副作用。

Seriously consider a different design for your app. 认真考虑为您的应用设计不同的设计。

With that in mind, here's probably the stupidest hack I'll post all day: 考虑到这一点,这可能是我整天都会发布的最愚蠢的骇客:

public partial class Form1 : Form
{

    Form2 f2;
    System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();

    public Form1()
    {
        InitializeComponent();
        tmr.Interval = 100;
        tmr.Enabled = false;
        tmr.Tick += delegate (object sender, EventArgs e) {
            tmr.Stop();
            this.IsMdiContainer = false;
        };
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (f2 != null)
        {
            MessageBox.Show("Close form!");
            return;
        }

        f2 = new Form2();
        f2.FormClosed += delegate(object sender2, FormClosedEventArgs e2) { 
            f2 = null; 
        };    
        if (radioButton1.Checked == true)
        {
            this.IsMdiContainer = true;
            f2.FormClosed += delegate(object sender3, FormClosedEventArgs e3) { 
                tmr.Start();
            };    
            f2.MdiParent = this;
        }
        f2.Show();
    }

}

*I originally tried Invoking the call to change IsMdiContainer but that didn't work, so I switched to the Timer. *我最初尝试调用更改IsMdiContainer的调用,但是那没有用,所以我切换到了Timer。 Stupidity that works. 愚蠢的作品。 Use this solution with caution... 谨慎使用此解决方案...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM