显示/隐藏在C#中的主要形式 [英] Showing/Hiding the main form in C#

查看:186
本文介绍了显示/隐藏在C#中的主要形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在哪里上的按钮,点击Form1上会打开窗体2的程序。然后,我会,直到Form2的关闭隐藏Form1上。我的问题是,我不能让Form1以显示窗体2结束后。 ?如何解决这个问题。

I am working on a program where clicking on a button on form1 will open form2. I will then hide form1 until form2 is closed. The problem I have is that I cannot get form1 to show after form2 closes. Any ideas on how to fix this?

        try
        {
            Form1.ActiveForm.Hide();
            AddGradeForm = new Form2(Form.NumberOfSelections);
            AddGradeForm.ShowDialog();
            MessageBox.Show(AddGradeForm.Result.ToString());
        }
        catch (Exception i)
        {
            Form1.ActiveForm.Hide();
            AddGradeForm.Dispose();
            AddGradeForm = new Form2(Form.NumberOfSelections);
            AddGradeForm.ShowDialog();
            MessageBox.Show(AddGradeForm.Result.ToString());
        }
        Form1.ActiveForm.Show();



错误:NullReferenceException异常被unhanded。对象引用未设置到对象的实例。

ERROR: NullReferenceException was unhanded. Object reference not set to an instance of an object.

推荐答案

这是因为没有活动的形式了,你已经隐藏了一个可以是活动的。这有其他副作用,您的应用程序将失去焦点。你需要做的是前的对话框关闭跟踪以前的活动形式,并得到它再次显示。像这样的:

That's because there is no active form anymore, you've hidden the one that could be active. This has other side effects, your app will lose the focus. What you need to do is keep track of the previously active form and get it to show again before the dialog closes. Like this:

        var prior = Form.ActiveForm;
        using (var dlg = new Form2()) {
            dlg.FormClosing += delegate { prior.Show(); };
            prior.Hide();
            if (dlg.ShowDialog() == DialogResult.OK) {
                MessageBox.Show("result");
            }
        }

这篇关于显示/隐藏在C#中的主要形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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