Another small question - closing all forms except one

C

C

I would like to close (unload or hide) all forms except one Form2,
which calls the subroutine to do this.

Is there a simple way somewhat like this?

For Each Form
if Form <> Form2 then Form.Unload
Next

Thanks.
 
A

Armin Zingler

Am 01.08.2010 21:37, schrieb C:
I would like to close (unload or hide) all forms except one Form2,
which calls the subroutine to do this.

Is there a simple way somewhat like this?

For Each Form
if Form <> Form2 then Form.Unload
Next

Is Form2 shown modeless (.Show) or modally (.ShowDialog)?
Do you use the Application Framework? If yes, what is the
startup Form? If no, what is the startup object? If it's
Sub Main, how does the call to Application.Run look like?
How are the other Forms shown (modeless/modally)? Do you
use MDIForms?
 
C

C

Am 01.08.2010 21:37, schrieb C:




Is Form2 shown modeless (.Show) or modally (.ShowDialog)?

Form2 is shown with .Show and .BringToFront, so I guess it is
modeless.
Do you use the Application Framework?

What is that? *.exe?
If yes, what is the
startup Form?

The startup form is Form1.
If no, what is the startup object? If it's
Sub Main, how does the call to Application.Run look like?
How are the other Forms shown (modeless/modally)? Do you
use MDIForms?

There are no MDI forms.
 
J

J.B. Moreno

Form2 is shown with .Show and .BringToFront, so I guess it is
modeless.


What is that? *.exe?


The startup form is Form1.

If you close the startup form, you close the application.
 
C

C

If you close the startup form, you close the application.

I would like to close or hide all the forms except Form2. If closing
Form1 will cause a crash, I should just hide it, and close (unload)
the rest.

In VB6, I could unload Form1 without causing the program to crash.
Form2 is where most of the work was done.
 
C

Cor

Like Armin wrote, there are many ways (I did it already to you in another
thread) to start a Form application.

Probably you are better of with a separated Sub Main Module as start

Module Module1
Sub Main()
System.Windows.Forms.Application.EnableVisualStyles()
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(False)
System.Windows.Forms.Application.Run(New Form1())
End Sub
End Module



C said:
If you close the startup form, you close the application.

I would like to close or hide all the forms except Form2. If closing
Form1 will cause a crash, I should just hide it, and close (unload)
the rest.

In VB6, I could unload Form1 without causing the program to crash.
Form2 is where most of the work was done.
 
A

Armin Zingler

Hi,

let's start from the beginning...:

Every process consist of at least one thread. Every thread
has a main procedure. If that main procedure ends, the thread dies.
ASA all thread died, the process quits.

A UI thread is a thread that creates windows (Forms/Controls).
As Windows is event based, you need a message loop to process the
windows messages and keep a UI thread alive.
(http://msdn.microsoft.com/en-us/library/ms632590(VS.85).aspx)

The most flexible way to create a UI thread main procedure
is to write it on your own. The method containing the message loop
is called Application.Run(). So the minimum UI thread procedure is:

sub main
application.run()
end sub

However, this does not take you very far because there is no Form visible
yet. So, let's show a window:

sub main
dim f as form

f=new form
f.show
application.run()
end sub

Now it makes sense but application.run does not exist, yet. The message loop
would run forever. Therefore, there is an overloaded version of Run:

sub main
dim f as form

f=new form
f.show
application.run(f) 'passing a Form
end sub

This makes the message loop exit as soon as the passed Form has been closed.

In addition, we often want to use visual styles. This feature has to be enabled
before the first Form is created. Also, there's a feature called "compatible
text rendering" that is usually disabled. So, the full code is:

sub main
dim f as form
application.EnableVisualstyles
Application.SetCompatibleTextRenderingDefault(False)
f=new form
f.show
application.run(f) 'passing a Form
end sub


This kind of Sub Main has been considered as the most common Sub Main. Therefore,
(and for other purposes) the "Application Framework" has been introduced. It is
enabled by default in the project properties (on the "application" tab). If enabled,
you can specify a Form as the startup object. The compiler automatically compiles
a sub main that looks like the version above. This is to save work for the developer
for common startup scenarios. If the startup Form closes, the thread quits because
there's not code after application.run(f) anymore.

You can change the "shutdown mode" of the application on the same tab.
Either choose "When startup form closes" or "On last window close". As you write
"The startup form is Form1", it's probably the latter.

The other diaper for us hobby programmers - as MSFT considers VB developers - is the
"My" namespace. I don't elaborate on this because I completely decline it. You can make
your own decision:
http://msdn.microsoft.com/en-us/library/5btzf5yk(VS.90).aspx

I mention the My namesapce only because the Application Framework ist closely bound to it,
i.e. if you disable the My namespace, the Application Framework does not work anymore.
IMHO, My.* is like a intransparent blain inserted into your own project, and 90% of it
is dispensable. For example, there are many file IO classes/methods insinde the
System.IO namespace. The My namespace, introduced in VB 2005, duplicates most of what
already exists. It's just at detour. Instead of writing

Dim content = My.Computer.FileSystem.ReadAllBytes("blah")

you should better directly call

Dim content = IO.File.ReadAllBytes("blah")


Back to your problem: If you don't disable the My namespace like me, you can make
use of the OpenForms property:

My.Application.OpenForms

As the collection changes by closing the forms - which can be a problem when using
an enumerator - I'd put them into a list, first:

Dim forms As New List(Of Form)

For Each Form As Form In My.Application.OpenForms
If Not TypeOf Form Is Form2 Then 'exclude Form2
forms.Add(Form)
End If
Next

For Each Form In forms
Form.Close()
Next


Enough for now. ;-)
 
A

Armin Zingler

Am 02.08.2010 11:13, schrieb Cor:
Like Armin wrote, there are many ways (I did it already to you in another
thread) to start a Form application.

Probably you are better of with a separated Sub Main Module as start

Module Module1
Sub Main()
System.Windows.Forms.Application.EnableVisualStyles()
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(False)
System.Windows.Forms.Application.Run(New Form1())
End Sub
End Module

Sry Cor, I didn't want to copy from you. I was only too slow. :-/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top