Solved

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

Posted on 2007-03-27
12
474 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

19 Experts available now in Live!

Get 1:1 Help Now