Solved

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

Posted on 2007-03-27
12
475 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now