Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to close two forms at the same time? C# Window Form?
i have 3 forms open. but i only want to close 2,, i tried using first i call the 2nd form that i want to be closed

C#
form1 f = new form1(); //this will call the 2nd form
this.close(); //this will close the current form
f.show(); //this will show the 2nd form
f.close(); //this will close the 2nd form


but the 2nd form that i wanted to be close is still open.
Posted
Updated 27-Jul-20 6:04am
v5

You need to close that form from itself or an object that has access to it.

What object opened these forms? It can listen to the event you want and close the all the forms that need be.

e.g.

C#
partial class Form1 : Form
{
   Form2 _form2;
   Form3 _form3;
   private void MakeForms()
   {
      _form2 = new Form2();
      _form2.SomeEvent += HandleSomeEvent;

      _form3 = new Form3();
   }

   private void HandleSomeEvent(object sender, EventArgs args)
   (
      _form2.Close();
      _form3.Close();
   )
}

...
//Other file
partial class Form2 : Form
{
   public event EventHandler SomeEvent;

   //Call this method when you want to close the forms
   private void RaiseSomeEvent()
   {
      var handler = SomeEvent;
      if(SomeEvent != null)
          SomeEvent(this, EventArgs.Empty);
   }
}
...
//Other File
partial class Form3 : Form
{
}
 
Share this answer
 
Comments
cloud_garcia 19-Jul-12 21:24pm    
can you please give more example.. i really need this..
[no name] 20-Jul-12 9:33am    
Hmmm. Not sure how much more clear it could be. Not sure what part you are not getting. Everything you need is there.
Rahul Rajat Singh 20-Jul-12 0:15am    
+5.
What event is triggering the close of the two windows?

This.Close() will only close the form on which the code is written. That's what the "This" part of it means. So if you have a button on Form3 named btnExecute and you want it to close Form1 and Form2, you would have to do something like this: (it's in vb.net, but you should easily be able to convert it)
VB
Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecute.Click
    Form1.Close()
    Form2.Close()
End Sub



UPDATE - It's really easy to switch from VB.Net to C#....here is the C#:
C#
private void btnExecute_Click(object sender, System.EventArgs e)
{
   Form1.Close();
   Form2.Close();
}
 
Share this answer
 
v2
Comments
[no name] 19-Jul-12 13:53pm    
Since the "C#" tag was used you should really give your example in "C#"
Kschuler 19-Jul-12 13:56pm    
I didn't want to take ALL the work out of it for the OP. :)
cloud_garcia 19-Jul-12 21:24pm    
can you give a c# code example. please
Kschuler 20-Jul-12 8:31am    
I updated my solution.
in case you are using nested form (form within form) for example login form within Form1 we can pass the Form1 object to loginpage form constructor as,
file: Form1.cs
private void iconButton1_Click(object sender, EventArgs e)
{
OpenChildForm(new loginpage(this));
}


private void OpenChildForm(Form ChildForm)//refered from rjcode
{
if(CurrentChildForm != null)
{
CurrentChildForm.Close();
}
CurrentChildForm = ChildForm;
CurrentChildForm.TopLevel = false;
CurrentChildForm.FormBorderStyle = FormBorderStyle.None;
CurrentChildForm.Dock = DockStyle.Fill;
panel2.Controls.Add(CurrentChildForm);
panel2.Tag = CurrentChildForm;
CurrentChildForm.BringToFront();
CurrentChildForm.Show();
}

file: loginpage.cs
Form1 mainform;
public loginpage(Form1 main)
{
InitializeComponent();
mainform = main;
}
private void button1_Click(object sender, EventArgs e)
{
MainPage nextForm = new MainPage();
mainform.Hide();
nextForm.ShowDialog();
mainform.Close();

}
 
Share this answer
 
Comments
Richard MacCutchan 27-Jul-20 12:23pm    
This was solved eight years ago.
Member 14900177 27-Jul-20 12:26pm    
this is in case of nested forms and closing the nested form doesn't close the main form(Form1.cs), since the form object not passed. we solve passing form object to nested form through constructor
when you show your page when make object then
form f1=new form();
this.hide();
f1.showdialog();
this.close();
i think it is helpful for you
 
Share this answer
 
Comments
cloud_garcia 19-Jul-12 21:19pm    
i already try this but the 2nd form is still open

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900