Skip to content
Loading
  • Sourav Kumar Das
    +1
     
     
    It's Showing same error...@Amit Gupta
    +1
  • Sourav Kumar Das
    +1
    I accept your previous solution but the background form is not closing.
    @Amit Gupta
     
     
    +1
  • Sourav Kumar Das
    +1
    But we are only closing specific form at your given solution....
    Because if it implements in MDI form in which it has multiple forms, so at that time how i can use it..
    @Amit Gupta
    +1
  • Sourav Kumar Das
    +1
    using MDI technique
     
    If there's a solution please send me the link.....
    +1
  • +1
    1. private void btnNewForm_Click(object sender, EventArgs e)  
    2. {  
    3. Form newForm = new Form();  
    4.   
    5. this.Hide();  
    6. newForm.Show();
    7. }  
    +1
  • Dorababu Meka
    +1
    Try this code
    1. List
       openForms = new List();  
    2.   
    3. foreach (Form f in Application.OpenForms)  
    4.     openForms.Add(f);  
    5.   
    6. foreach (Form f in openForms)  
    7. {  
    8.         f.Close();  
    9. }   
     Or
    1. Form[] forms = Application.OpenForms.Cast().ToArray();    
    2. foreach (Form thisForm in forms)    
    3. {    
    4.     thisForm.Close();    
    5. }  
     
    +1
  • Gaurav Gahlot
    +1
    Hi Sourav!
     
    You are getting an error, because you are modifying the collection that you are currently itterating on. So, this is surely not going to work.
     
    Take a note of the following points:
    • closing Form1, which the main form in your case, will close the application
    • you should not close Form1, rather hide it
    • How to hide?  As rightly pointed by @Rama, you should not itterate over the OpenForms because it is not efficient. So, you can do somethng like:
    1. private void btnNewForm_Click(object sender, EventArgs e)  
    2. {  
    3.         // hide the current form, Form1  
    4.         this.Hide();  
    5.   
    6.         var newForm = new Form2();  
    7.         newForm.Show();  
    8. }  
    This should solve, your errors. However, the solution might wary based on your usecase. 
     
    I hope this helps.
     
    Thank you! 
    +1
  • Sreekanth Reddy
    +1
    @Rama Krishna
    Can I know. What the below line refers to which you had stated ?
    form.Closed += (s, args) => this.Close();
    +1
  • Sreekanth Reddy
    +1
    I found the below solution in stack overflow :
    https://stackoverflow.com/questions/9029351/close-all-open-forms-except-the-main-menu-in-c-sharp
    List openForms = new List();
    foreach (Form f in Application.OpenForms)
    openForms.Add(f);
    foreach (Form f in openForms)
    {
    if (f.Name != "Menu")
    f.Close();
    }
    Or you can use a for loop:
    for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
    {
    if (Application.OpenForms[i].Name != "Menu")
    Application.OpenForms[i].Close();
    }
    +1
  • Gaurav Gahlot
    +1
    @ Sreekanth
    1. form.Closed += (s, args) => this.Close();  
     
    This line of code binds an anonymous method with the Closed event of form. So, when the closed event will be raised or triggered, this method will handle that event.
    +1