a bit of messing and i created this simple code to allow you to unload all open forms before closing the App's main form in VB.NET
in a Module :
VB Code:
Module Module1
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const WM_CLOSE As Integer = CInt(&H10)
Public Function CloseForms(ByVal forms() As String) As Boolean
Dim x As Integer
Dim i As Integer
For i = LBound(forms) To UBound(forms)
x = FindWindow(vbNullString, forms(i))
If Not x = 0 Then
PostMessage(x, WM_CLOSE, 0, 0) '/// close the Form.
End If
Next
Return True
End Function
End Module
in your Main Form ( eg: Form1 ) :
VB Code:
Private frms() As Form = {New Form2(), New Form3()} '/// all forms in the Application apart from Form1.
Private frmNames(1) As String '/// this will house the window caption of the forms.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = LBound(frms) To UBound(frms)
frmNames(i) = frms(i).Text
Next
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If CloseForms(frmNames) = True Then
Me.Close()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frms(0).Show()
frms(1).Show()
MyBase.BringToFront()
End Sub
i've included a demo sample project ...
~ if a post is resolved, please mark it as [Resolved] protected string get_Signature(){return Censored;} [vbcode][php] please use code tags when posting any code [/php][/vbcode]
[color=blue]Catch[/color] ex [color=blue]As[/color] Exception
MessageBox.Show("Oops, the following error occured:" & ex.Message)
[color=blue]Finally[/color]
[color=blue]MyBase[/color].Close() '/// now that all the other forms are closed , unload this one.
[color=blue]End Try[/color]
[color=blue]End Sub[/color]
~ if a post is resolved, please mark it as [Resolved] protected string get_Signature(){return Censored;} [vbcode][php] please use code tags when posting any code [/php][/vbcode]
Oops forgot to add the attachment sample code .zip
~ if a post is resolved, please mark it as [Resolved] protected string get_Signature(){return Censored;} [vbcode][php] please use code tags when posting any code [/php][/vbcode]
Application.Exit() does NOT raise the Form.Close() event and can leave processes of a Form running in the memory ( thus is classed as unsafe ) , before running Application.Exit() you must use Form.Close() on each Form. here's a quote direct from MSDN ...
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.
it's bad practise in VB.NET to just use Application.Exit() or End.
but if you wish to use unsafe methods which are NOT recommended by microsoft , who am i to argue
~ if a post is resolved, please mark it as [Resolved] protected string get_Signature(){return Censored;} [vbcode][php] please use code tags when posting any code [/php][/vbcode]