954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to unload all open forms

Hi All,
I need some guidance here...How does one close or unload all open forms in VB.Net. Can someone please help me out in this regard?
Thanks very much.

hari12341
Newbie Poster
6 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

Application.Exit() :D

Inanna
Junior Poster in Training
90 posts since Oct 2006
Reputation Points: 53
Solved Threads: 6
 

i may try that out but this is the warning issued in msdn under application.exit() method

CAUTION The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

hari12341
Newbie Poster
6 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

I used a for in vb to close all open forms but i am new to .Net and have not been able to do the same in .Net
Code in VB:

for each frm in forms
frm.close()
next


In .Net this does not work i guess...

hari12341
Newbie Poster
6 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

hmn:

for each XFrm in forms
     unload XFrm
next XFrm


?

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 
CAUTION The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.


If you're using .NET 2.0 then that's not the case anymore. Application.Exit() raises the Form.Closing events.

Inanna
Junior Poster in Training
90 posts since Oct 2006
Reputation Points: 53
Solved Threads: 6
 

This is for VB Express 2005:

For i As Integer = My.Application.OpenForms.Count - 1 To 0 Step -1
            If My.Application.OpenForms.Item(i) IsNot Me Then
                My.Application.OpenForms.Item(i).Close()
            End If
        Next i
waynespangler
Posting Pro in Training
461 posts since Dec 2002
Reputation Points: 84
Solved Threads: 58
 
If you're using .NET 2.0 then that's not the case anymore. Application.Exit() raises the Form.Closing events.


Application.Exit() causes all the dialogs to get closed. but, It creates some problems in my test applicatin. is there anyother way to close all the opened forms by the current Application..??

syeedsan
Newbie Poster
1 post since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

end

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

Well I've try also the code below:

Dim openForm As Form = Nothing

For index As Integer = My.Application.OpenForms.Count - 1 To 0 Step -1
    openForm = My.Application.OpenForms.Item(index)
    If openForm IsNot Me AndAlso Not TypeOf openForm Is DevExpress.Utils.Win.TopFormBase Then
      openForm.Close()
      openForm.Dispose()
      openForm = Nothing
    End If
Next


but there were some forms that were not close or rather I say was not detected as openforms.

My question is when during opening the form and it encounters some errors but still able to load the form, does the form considered as not an openform?

jireh
Posting Whiz
316 posts since Jul 2007
Reputation Points: 11
Solved Threads: 49
 

thanks for the post it helped a lot =)

yassha
Newbie Poster
1 post since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

So simple dude.. If you want to close all the open forms just type Application.Exit().
Or else if you want to go for beginning then type Application.Restart()

sathish_ahs10
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You