Solved

Closing all forms except the main

Posted on 2006-10-21
7
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

696 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