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.

Recommended Answers

All 11 Replies

Application.Exit() :D

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.

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

hmn:

for each XFrm in forms
     unload XFrm
next XFrm

?

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.

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

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

end

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?

thanks for the post it helped a lot =)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.