Solved

Closing all forms except the main

Posted on 2006-10-21
7
172 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question! 4 38
C# winforms programmitically move panels 6 42
LINQ Select all records by MAX data syntax help 1 17
Change to file doesn't show up 16 50
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

791 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