473,382 Members | 1,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Close all forms but Main

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
Jun 30 '06 #1
6 9265
Check out the Application.OpenForms Property.

Tony

"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

Jun 30 '06 #2
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.)

Jul 3 '06 #3
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

"Kevin Yu [MSFT]" <v-****@online.microsoft.comwrote in message
news:vT**************@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.)

Jul 10 '06 #4
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.)

Jul 11 '06 #5
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.

"Kevin Yu [MSFT]" <v-****@online.microsoft.comwrote in message
news:N8**************@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.)



Jul 11 '06 #6
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.)

Jul 11 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: chanchito_cojones | last post by:
hi there, I am needing some help with a database I am putting together. The database works off of a main Form, which then has buttons on it that will open up other forms. The problem I am having...
2
by: Guy Babbitt | last post by:
I have an MDI application that starts an instance of a child form at application start. I have an event handler on a combo box checking for the selected value to change. When the select value...
1
by: Ebrahim | last post by:
This message is in reply to a prev 1 . My application refues to close . Some one had suggested that I might have threads running.. but i solved that problem too . The app still refuses to close...
1
by: Alpha | last post by:
I have a Window based application that shows up still running in the task manager when I close it. It reaches the "this.close" statement and then it stops at the "}" at the section of the...
2
by: Sunil Sabir | last post by:
Dear All, I have a Main Asp.Net page . In that Page I have a Tab Strip which has SIX tabs each representing a different web Forms.So that when a user clicks different tab he can navigate between...
7
by: Alice | last post by:
Hi, In my program, the user can navigate to many different forms. When they go to the next form, I want the form they have left to close. However, the forms aren't closing. Can anyone tell me...
3
by: Karan | last post by:
I am calling finalize when form2 loads and deactivates form1 which closes form1. However, same thing is not happening in form2 because finalize is already called. Does anybody has solution to it....
4
by: Jason Huang | last post by:
Hi, In my C# windows form project, how do I in a formA open another formB, and close formA when formB is opened? Thanks for help. Jason
6
by: dotnetnovice | last post by:
Hi everybody. I need some help in c# forms problem which is i have three forms naming main form,form1 & form2. main form is the mdiparent of both form1 and form2. i also have a toolstrip in my...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.