?
Solved

Closing all forms except the main

Posted on 2006-10-21
7
Medium Priority
?
175 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 500 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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
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…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

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