Solved

Closing all forms except the main

Posted on 2006-10-21
7
169 Views
Last Modified: 2010-04-16
Hi,
I have a application there from a main form several other forms can be created ( and hided ) by user and  from them other forms can be created etc
To start a new calculation ( a new case) with the program I have to be sure that all forms (visibled and hided) are cleaned from the memory.
Alternative is, of course, to close, the main form ( and the program) and restart it again.  
How I can cleaned all this forms except the main one (to avoid restarting of the program)?
I use VS 2005

Thanks
Ted
0
Comment
Question by:teja21
  • 4
  • 3
7 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17780622
"I use VS 2005"

Use the Application.OpenForms collection...

(assuming this code is run from the "main" form itself)

        private void button1_Click(object sender, EventArgs e)
        {
            ArrayList ToBeClosed = new ArrayList();
            foreach (Form frm in Application.OpenForms)
                if (frm != this)
                    ToBeClosed.Add(frm);
            foreach (Form frm in ToBeClosed)
                frm.Close();
        }
0
 

Author Comment

by:teja21
ID: 17781067
It's working fine (the forms disappeared) but the forms are still somewhere in the memory:   when I recalculated the program with another case
, the data file is changed (which  put data into  the new forms) but the forms is started with the old data !
//Ted

PS. I start the forms with e.g.
  Form11.Owner =this;
  Form11.Show();
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17781178
You need to create the forms using the "new" keyword:

    Form11 f11 = new Form11();
    f11.Owner = this;
    f11.Show();
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:teja21
ID: 17781226
Yes you  are right (I create the forms by" new"  in the start of the program, but:

why I can show the "old" forms when the "new " are not created - that means that the old ones are still in the memory !
That is not good : user in my application can easy start up 50-100 forms/ per case.
After a couple of cases .. I dont want to keep all that in the computer.
//T
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17781706
If you are creating new forms each time and data is still there then something else is going on.

Are you loading data into the forms in the load event or anything like that?

Show us more code...
0
 

Author Comment

by:teja21
ID: 17784387
Let me start from the beginning. The "Large" forms are created (by key 'new') on the main form at the program start.
The "small"  forms  are created on the "Large" forms (by 'new') at the forms start.
That means that I have all forms created at the start og the program (right?)

All forms are predefined with local function, parameters, and updated later  by "loading botton" which read a file and  via a funciton call
updates the forms. The external call  like:
int ret10 = formLarge .Pisz(STGi1,ParVmaxS1,.................,stag,van);
                        switch (stag)
                        {
                              case(1):
                                    fL.Owner =this;
                                    fL.Show();
                                    break;  
.                                                                          ...........................................

 // fL = new formLarge();

I relize that have to change creation of the Large form from the main form to somewhere in the "loading botton" before the forms will be updated
with the new data.
Anyway it is still a question what happen with the old forms (from the previous case) .Atrial with your snipett indicate that the form are still in the memory)
Thanks
Ted
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 17785863
You could try calling Dispose() on each form as well:
 
            ArrayList ToBeClosed = new ArrayList();
            foreach (Form frm in Application.OpenForms)
                if (frm != this)
                    ToBeClosed.Add(frm);
            foreach (Form frm in ToBeClosed)
            {
                frm.Close();
                frm.Dispose();
            }
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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…

758 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