Solved

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

Posted on 2007-03-27
12
476 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

831 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