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

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.
lapuccaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NetworkArchitekCommented:
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
lapuccaAuthor Commented:
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
gregoryyoungCommented:
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
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

lapuccaAuthor Commented:
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
NetworkArchitekCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
angus_young_acdcCommented:
Application.Exit();

That will close all forms including the invisible ones for you.
0
angus_young_acdcCommented:
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
NetworkArchitekCommented:
I already mentioned Application.Exit(). I was assuming there was some necessary clean-up needed ...
0
lapuccaAuthor Commented:
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
lapuccaAuthor Commented:
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
lapuccaAuthor Commented:
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
angus_young_acdcCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.