如何使用C#在mdi中单击另一个子表单时关闭上一个子表单 [英] How to open close the previous child form on clicking another child form in mdi using C#

查看:71
本文介绍了如何使用C#在mdi中单击另一个子表单时关闭上一个子表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

In my application i have one parent form and two child forms for that parent form here my parent form is Form1 and
my child forms are Upload and Reports on clicking upload i need to close reports form and if i click reports i need to close
upload form how can i do this below is my code

<pre lang="c#">private void winAppToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Upload objWA = new Upload();
            objWA.MdiParent = this;
            objWA.Show();
            //objWA.WindowState = FormWindowState.Maximized;
        }

        private void userInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Reports objUI = new Reports();
            objUI.MdiParent = this;
            objUI.Show();
            //objUI.WindowState = FormWindowState.Maximized;
        }





我的尝试:



What I have tried:

how can i colse the previous child form if i had opened another child form

推荐答案

我怎么能收集上一个子窗体?首先要做的是注意你想要保持对这两种形式的引用:移动objWA和objUI(它们都被严重命名 - obj是一个非常糟糕的前缀,即使建议用于C#的伪匈牙利表示法,但它不是)在方法之外作为私有变量进入MDI父表单类。

将它们初始化为null,当您处理单击事件并从那里发出相应的关闭指令时检查null。您还需要处理表单关闭事件:

The first thing to do is to note that you want to keep references to the two forms: move the objWA and objUI (which are both badly named - "obj" is a very bad prefix, even if pseudo-Hungarian notation was recommended for C#, which it isn't) outside of the methods and into the MDI parent form class as private variables.
Initialize them to null, check for null when you handle you click events and issue the appropriate close instructions from there. You will also need to handle the form close event:
private Upload formUpload = null;
private Reports formReport = null;
...
private void winAppToolStripMenuItem_Click(object sender, EventArgs e)
    {
    if (formUpLoad != null) return;
    formUpload = new Upload();
    formUpload.MdiParent = this;
    formUpload.FormClosed += formUploadClosed;
    if (formReport != null) formReport.Close();
    formUpload.Show();
    }
private void formUploadClosed(object sender, EventArgs e)
    {
    formUpload = null;
    }

为另一个重复这个,你应该好好去。

Repeat that for the other from, and you should be good to go.


这篇关于如何使用C#在mdi中单击另一个子表单时关闭上一个子表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆