Solved

Closing all forms except the main

Posted on 2006-10-21
7
173 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 86

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 86

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 86

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 86

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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