Home All Groups Group Topic Archive Search About

Close all forms but Main

Author
30 Jun 2006 4:35 PM
Ryan
My application has a main form from which the user can use the Menu to open
other forms.  Each form opens in a separate window while the main form
remains open all the time.  Is their any code I can place in Form_Load for
all my other forms (besides Main) that will close all other open forms (if
any exist) except for the Main form?  Something like For Each Form in
Windows.Forms??  What's the code?'

Thanks,
Ryan

Author
30 Jun 2006 8:33 PM
tlkerns
Check out the Application.OpenForms Property.

Tony

Show quoteHide quote
"Ryan" wrote:

> My application has a main form from which the user can use the Menu to open
> other forms.  Each form opens in a separate window while the main form
> remains open all the time.  Is their any code I can place in Form_Load for
> all my other forms (besides Main) that will close all other open forms (if
> any exist) except for the Main form?  Something like For Each Form in
> Windows.Forms??  What's the code?'
>
> Thanks,
> Ryan
>
>
>
Author
3 Jul 2006 12:55 AM
Kevin Yu [MSFT]
Hi Ryan,

Yes, you can go through the Application.OpenForms collection and close all
of them, except the main form. You can check the Form.Text property for the
main form.

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Author
10 Jul 2006 6:40 PM
Ryan
I tried this code and I get an error everytime a Form closes because the
objects in My.Application.OpenForms change within the for loop.

Private Sub Application_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

For Each f As Form In My.Application.OpenForms

If f.Name <> "MainForm"  Then

f.Close()

End If

Next

End Sub



Show quoteHide quote
"Kevin Yu [MSFT]" <v-k***@online.microsoft.com> wrote in message
news:vT5P1tjnGHA.4612@TK2MSFTNGXA01.phx.gbl...
> Hi Ryan,
>
> Yes, you can go through the Application.OpenForms collection and close all
> of them, except the main form. You can check the Form.Text property for
> the
> main form.
>
> Kevin Yu
> Microsoft Online Community Support
>
> ============================================================================
> ==========================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ============================================================================
> ==========================
>
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
Author
11 Jul 2006 2:42 AM
Kevin Yu [MSFT]
Hi Ryan,

You can take advantage of GoTo and Try/Catch block. Not very graceful, but
it does work. :-)

        Try
a:
            For Each f As Form In My.Application.OpenForms

                If f.Name <> "Form1" Then

                    f.Close()

                End If

            Next

        Catch
            GoTo a
        End Try

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Author
11 Jul 2006 3:06 AM
Michael D. Ober
Dump the goto:

dim Success as Boolean
do
  success = true
  try
      for each f as form in my.application.openforms
           if f.name <> "Form1" then f.close
      next f
  catch ex as exception
      success = false
  end try
loop until success

With structured exception handling, Goto should generate a compiler warning
"Warning:  Speghetti Code using Goto".  Since you can jump an arbritrary
number of levels up via try ... catch blocks, there is no longer any need in
the VB language for Goto.

Mike Ober.

Show quoteHide quote
"Kevin Yu [MSFT]" <v-k***@online.microsoft.com> wrote in message
news:N8sDvOJpGHA.2024@TK2MSFTNGXA01.phx.gbl...
> Hi Ryan,
>
> You can take advantage of GoTo and Try/Catch block. Not very graceful, but
> it does work. :-)
>
>         Try
> a:
>             For Each f As Form In My.Application.OpenForms
>
>                 If f.Name <> "Form1" Then
>
>                     f.Close()
>
>                 End If
>
>             Next
>
>         Catch
>             GoTo a
>         End Try
>
> Kevin Yu
> Microsoft Online Community Support
>
>
============================================================================
> ==========================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
>
============================================================================
> ==========================
>
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>
Author
11 Jul 2006 4:52 AM
Kevin Yu [MSFT]
Thanks, Michael! Looks better.

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)