Solved

How do I close all the forms that are called by other froms and are invisible?

Posted on 2007-03-27
12
478 Views
Last Modified: 2010-05-18
Hi, I'm using VS2005 and .net 2.0.  My main form has a Next button which makes the main form invisible and starts a new form which I'll call form2.  The form 2 has a Previous and Next button.  The previous button would make the main form visiable again.  The next button would invoke a form3.  both of these buttons would make form2 invisible.  The form3 has a previous button to go back to form2 vislble and make itself invisible.  
My question is, if the user is closing the application either at form2 or form3  then how do I make sure all forms are closed?  Would coding to close the main form automatically close all the chidren form in the approperate order?  If not, how can I address this?  Becuase right now it's causing memory correuption in my application.  Thanks.
0
Comment
Question by:lapucca
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 18804666
It would close all the forms but I would not count on a particular order. Why does it matter?

Anyway, if you have to have this sort of functionality then this is what I suggest. Whenever you create a new form put it in an ArrayList object that is a member of your class.  Then, on your main form's "Closing" event, you can run through the arraylist like:

foreach(Form frm in myArrayList)
{
      if(frm.Visible == false)
            // .... do stuff
}

0
 

Author Comment

by:lapucca
ID: 18804760
The problem I have now is when I close at form3 or form2, the application is still running becuase the formMain is still there but is just invisible.  I have to click the stop button in VS in debug mode to stop the application but then it corrupts the memory.  How can I address this problem?  Thank you.
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 100 total points
ID: 18804931
well the easy answer would be to not do this....

The intented use would be for you to place controls in your main form for each step as opposed to creating a new form for each step.

Doing this would remove your observed problems and be lighter overhead for the system your app runs on. It would also be a more flexible design for meeting future needs.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lapucca
ID: 18805310
It just seems so difficult to figure out the location of the new controls.  Also, there are data on each of the form that I want to retain or sometimes refresh.  
0
 
LVL 10

Accepted Solution

by:
NetworkArchitek earned 400 total points
ID: 18805735
Ok, this one is a bit of a complicated setup. All right, if you really want to do this then you can one of two things. Either,

1)  In the Closing event of each of those forms (form2, form3) then you can call Application.Exit();

or

2)  On your formMain definition, place a method that will do what I said before, close all your forms etc. Then, modify each of your subforms to have a constructor which has as a parameter an object of the same time as formMain. Then, in the Closing event of each of those forms, you can have it call the method you defined earlier in this step.

Probably, either choose #1 or redo this design substantially ...
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 18806943
Application.Exit();

That will close all forms including the invisible ones for you.
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 18806968
Oh forgot to say, if you have ControlBox set to False and have an Exit button (for example in a menu) that will work better without having your exit method.
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 18811426
I already mentioned Application.Exit(). I was assuming there was some necessary clean-up needed ...
0
 

Author Comment

by:lapucca
ID: 18812796
I change my code to use form.show() and form.Hide so that the formClosing events are not invoked as with the Visible propoerty.

I put Application.Exit() in the Closing event of each of those forms (form2, form3).  I debug through this from form2 and noticed that form3's FormClosing event was also called and it also has a Application.Exit() ;.  I hope that's OK that it's called twice.
But, as I debug through this, application ends at Program.cs at the following line:
Application.Run)new frmMain());
The debug then exits in VS but I still see my application in task manager and it won't go away with end task either.  I have to exit VS for it to remove the program from task manager.
0
 

Author Comment

by:lapucca
ID: 18813140
I tried using Application.Exist() at form2's FormClosed event and the same problem exists.  The application still shows up in the task manager.
0
 

Author Comment

by:lapucca
ID: 18813236
I put the code in the FormClosed event of 3 forms.  It checks to see if children forms were created then childForm.close(); is called.  I pass the calling form handle to both child forms, form2(this); which is receive as a static Form variable in the child form.  
I tried closing the form from form2 and the FormClosed was looped through many times and then I get a error message: An unhandled exception of type 'System.stackoverflowexception' occured in System.Windows.Forms.dll.  Why can't I close the calling form with the static form variable that stores the calling form's handle?  Also, the application stays in the task manager until I exit the VS.  Thank you.
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 18814985
Instead of making each form invisible why not just close them?  Form1 will need to be invisible of course, but you should then be able to Application.Exit() it to shut everything down.  For each form where you normally have to make it invisible have:
this.Close();
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question