C#程序里,父对话框调用子对话框,然后,点击子对话框的关闭按钮(叉号)的同时也关闭了父对话框,该怎么写?

解决方案 »

  1.   

    这方法很多啊,
    你可以在 初始化子窗体时候,在把父窗体的关闭方法加到子窗体的关闭事件里,子窗体就能触发关闭。你也可以直接在子窗体关闭的时候, this.Parent.Dispose()也能搞掉。其它的可以再试试。
      

  2.   

    parent:
    Form f = new Form();// f.FormClosed 事件 Lambada 表达式
    child.FormClosed += (obj, args) => { this.Close(); };f.Show(this);
      

  3.   


    如果不用 Lambada 表达式,那么需要在子窗口添加 FormClosed 事件的处理代码
    void Form_FormClosed(object sender, EventArgs e)
    {
        // 因为在父窗口调用的是 f.Show(this); 方法
        if(this.Owner != null) this.Owner.Close(); // 但愿不报错
    }