2016-03-29 107 views
3

我正在使用Visual Studio 2015中的窗体窗体应用程序。现在我的要求是关闭按钮单击时的多个窗体窗体。如何在Windows窗体中关闭多个窗体应用

注:我下面简称链接

closing-multiple-forms-in-c-sharp

How-to-Connect-forms-in-Csharp-windows-application

How to swap between forms in windows forms application

检查什么我已经做了以下图片。

图像 - 1 enter image description here

图像 - 2 enter image description here

图像 - 3 enter image description here

图像 - 4 enter image description here

+0

您创建了第一个形式的另一个实例和隐藏它,所以你目前打开的第一个表单不会隐藏。 您应该隐藏打开的实例。 –

+0

让你的表单从IDisposable继承,并使用()语句放置它们。 –

回答

0

形式 - 1写出下面的代码

enter image description here

形式 - 2写出下面的代码 enter image description here

2

您创建了第一个表单的另一个实例并将其隐藏,因此当前打开的第一个表单不会隐藏。

您应该隐藏打开的实例。要做到这一点,您可以使用Application.OpenForms找到它:

var first = Application.OpenForms.OfType<FirstForm>().FirstOrDefault(); 
if (first != null) 
    first.Hide(); 

您也可以再保持参照第一形式Program类,并使用该参考Hide它或它Show

请注意,Application.OpenForms返回可见表单,如果您隐藏表单,表单将不会再出现在集合中。

+0

谢谢。这在我的实时应用程序中适用于我。我得到了我的预期。 @Reza Aghaei。 –

1

其简单..当你调用窗口2,那么你必须通过Form1的参考和隐藏的主要形式,并关闭窗口2时,点击按钮1,开放frmshow ..

当你点击BtnAnalysis

...

private void BtnAnalysis_Click(object sender, EventArgs e) 
{ 
    Form2 frm2 = new Form2(); 
    frm2.Show(this);  // here the reference of form1 pass to the form2 
} 

在你的窗体2你必须做这样的代码......

private Form1 frm; 
    public Form2() 
    { 
     InitializeComponent(); 
    } 

    public void Show(Form1 frm1) 
    { 
     frm = frm1;  // here assign the reference of form1 to form for hiding purpose 
     this.Show(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     frmshow frmshw = new frmshow(); 
     frm.Hide();  // hide mainform i.e. form1 
     this.Close();  // close the form2 
     frmshw.Show();  // show the frmshow form 
    } 

其任职肯定......你一定要试试这个...如果你想源,然后联系 我通过我的电子邮件。 ketanvavadia @ gmail。COM

相关问题