PC Review


Reply
Thread Tools Rate Thread

Another small question - closing all forms except one

 
 
C
Guest
Posts: n/a
 
      1st Aug 2010
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.
 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      1st Aug 2010
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?


--
Armin
 
Reply With Quote
 
C
Guest
Posts: n/a
 
      2nd Aug 2010
On 1 elo, 22:51, Armin Zingler <az.nos...@freenet.de> wrote:
> 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)?


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.

>
> --
> Armin


 
Reply With Quote
 
J.B. Moreno
Guest
Posts: n/a
 
      2nd Aug 2010
<(E-Mail Removed)> wrote:

> On 1 elo, 22:51, Armin Zingler <az.nos...@freenet.de> wrote:
> > 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)?

>
> 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 you close the startup form, you close the application.

--
J.B. Moreno
 
Reply With Quote
 
C
Guest
Posts: n/a
 
      2nd Aug 2010
On 2 elo, 10:01, "J.B. Moreno" <pl...@newsreaders.com> wrote:
> *<wrong.addres...@gmail.com> wrote:
> > On 1 elo, 22:51, Armin Zingler <az.nos...@freenet.de> wrote:
> > > 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)?

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

> --
> J.B. Moreno- Piilota siteerattu teksti -
>
> - Näytä siteerattu teksti -


 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      2nd Aug 2010
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" <(E-Mail Removed)> wrote in message
news:c4513c66-39b4-4ef0-b97e-(E-Mail Removed)...
> On 2 elo, 10:01, "J.B. Moreno" <pl...@newsreaders.com> wrote:
>> <wrong.addres...@gmail.com> wrote:
>> > On 1 elo, 22:51, Armin Zingler <az.nos...@freenet.de> wrote:
>> > > 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)?

>>
>> > 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 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.
>
>> --
>> J.B. Moreno- Piilota siteerattu teksti -
>>
>> - Näytä siteerattu teksti -

>

 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      2nd Aug 2010
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. ;-)

--
Armin
 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      2nd Aug 2010
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. :-/


--
Armin
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Printer for 3x5 cards and other small forms Jeff Jonas Printers 13 8th Aug 2011 02:03 PM
trying to open two forms on maximized other usually max. but small babs Microsoft Access 7 27th Apr 2010 02:07 AM
Using small images in forms STiruchi Microsoft Access Forms 1 6th Sep 2008 04:40 PM
OPENING/CLOSING SCREENS - ARE SMALL Robert E. Willis Windows XP Basics 0 22nd Sep 2005 03:30 AM
Requery forms is closing certain forms. =?Utf-8?B?dGhlcnJpZW4=?= Microsoft Access Form Coding 0 6th Jul 2005 03:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:30 AM.