Close all forms in VB .net?

JonTheBaller

Golden Member
Dec 2, 2002
1,916
0
0
I am having a problem closing all forms in my VB .Net application. I have a Login form, which opens a subform called Main. Main has several forms which it may open (think of main menu). When I click on the X button in the top right corner in Login or Main, the app closes properly. However if I hit X in any other screen, it remains running but doesn't show any visible signs of this; I see that it is running in the task manager, but I don't see any open windows.

------------------------------------------------------------------------------------------------------------------------

When I login from Login to Main, I use the following code:

Dim mainForm As New Main(Me)
Me.Hide()
mainForm.Show()

------------------------------------------------------------------------------------------------------------------------

Main looks like this... the constructor and the close/finalize Subs are shown.

Public Class MapForm
Inherits System.Windows.Forms.Form

Dim parent_form As Form

Public Sub New(ByRef p As Form)
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
parent_form = p
End Sub

Public Sub Form_Closed(ByVal sender As System.Object, ByVal e As EventArgs) Handles MyBase.Closed
Finalize()
End Sub

Protected Overrides Sub finalize()
MsgBox("test")
Dim er As System.Exception
Try
Dim pack As New Packet()
pack.Type = Packet.LOGOUT
Packet.writePacket(stream, pack)
parent_form.Close()
Close()
Catch er
End Try
End Sub

------------------------------------------------------------------------------------------------------------------------

The relationship between the subforms of Main and Main is the same as Main and Login (in terms of the constructor and close/finalize). However, closing only works for Main and Login, and not the subforms of Main. So my question is, how do I fix this problem?

I may have been vague in my explanation, so if you have any questions please ask away.

Thanks!
 

bot2600

Platinum Member
May 18, 2001
2,075
0
76
I dont know if it is the cleanest way or not, but if you add an Application.Exit(); as the last line of your Dispose method of each form, that will close the app when any form is closed.

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
Application.Exit();
}

that is my Dispose method, it is in C# though, I used vb6, but have switched to c# for .net stuffs ;)
 

bot2600

Platinum Member
May 18, 2001
2,075
0
76
Here it is in VB.NET, not much different..

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
Application.Exit()
End Sub
 

JonTheBaller

Golden Member
Dec 2, 2002
1,916
0
0
Thanks! On a side note, is it possible to make input/output statements in that block of code? My program uses sockets, and I tried writing a packet to the host but it gives me an IO error.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
You really shouldn't be putting "cleanup" code in Dispose. Dispose is implicitly called on a Form instance when you Application.Exit(), so you need to perform the Application.Exit() in another function; perhaps as the result of a menu selection, etc.

 

JonTheBaller

Golden Member
Dec 2, 2002
1,916
0
0
Thanks Descartes.

My problem isn't when I exit the program properly, it is when someone hits the X in the upper right corner. In certain cases (as described in the first post), the program does not exit properly, and continues running despite no open windows.
 

bot2600

Platinum Member
May 18, 2001
2,075
0
76
Like I said earlier, I was just giving something I knew would work, not necessarily the best way. I dont know anything about the project. A "better" way would be to do it in the closing event of the form, then you could do "cleanup" code and your IO code if you wished. The is closing event gets fired earlier than disposing and the form still exists, so these tasks are possible, you could even stop the form from closing if you want. Th C# code is

private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{

}

You may have to get Descartes to tell you how to do it in VB, I have never done events in VB, I think they may call them delegates or something there.