-
Standard way to close all forms
Hi all,
I am using VS 2005 (.NET 2.0). However, I am fairly new to .NET so please bear with me. In VB6 it was pretty standard to have this in the cmdExit_Click event on frmMain:
Private Sub cmdExit_Click()
Dim frm As Form
For Each frm In Forms
Unload frm
Next frm
End Sub
I've been searching high and low for a generic, standard way to close all open forms in a Windows Application, but every example I find is different. I finally came up with this and it seems to work but I just wanted to get your feedback. Do you see anything wrong with doing it this way? Is there a simpler way? I already tried looping over the OpenForms collection using a For-Each loop, but it throws an error when you close the first form.
Thanks.
----------
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim f As Windows.Forms.Form
Dim i As Integer = My.Application.OpenForms.Count - 1
Try
Do While i > 0
'Close any other forms that may be open before frmMain closes...
f = My.Application.OpenForms.Item(i)
If f.Name <> Me.Name Then
f.Close()
i -= 1
End If
Loop
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly)
Finally
f = Nothing
End Try
End Sub
-
If your app has a main form (that is, you specify a startup form in your project's Application settings), you no longer need to write code to close all the other forms in your app. When the main form closes, all other forms close automatically.
Try this: create a new Windows Forms project; by default, Form1 becomes the startup form. Add the following code to the form:
Code:
Public Class Form1
Private Sub Form1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Click
Dim frm As New Form1
frm.Show()
End Sub
End Class
This will create and show a new instance of the form each time you click on it. Now run the project; click on the form a few times to open multiple windows. Now close the first form and watch what happens.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Very cool! So all I need is Me.Close in the btnExit_Click event.
Thanks!
Similar Threads
-
By Jacky in forum VB Classic
Replies: 4
Last Post: 08-13-2001, 07:54 AM
-
By Jacky in forum VB Classic
Replies: 0
Last Post: 08-10-2001, 11:38 PM
-
By Barend Esterhuizen in forum VB Classic
Replies: 1
Last Post: 06-16-2000, 04:39 AM
-
By Dajing Hu in forum VB Classic
Replies: 2
Last Post: 03-14-2000, 12:07 PM
-
By Dajing Hu in forum VB Classic
Replies: 0
Last Post: 03-14-2000, 11:15 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks