Close all forms but Main

R

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
 
K

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

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
 
K

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

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

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

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