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



Reply With Quote
...
