PDA

Click to See Complete Forum and Search --> : Unloading all Forms in VB.NET


dynamic_sysop
Jul 27th, 2003, 09:19 PM
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 :

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

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

bassist695
Jul 28th, 2003, 12:34 PM
Does the same logic apply to disabling all of the controls on a form?

Thanks,
Mike

dynamic_sysop
Aug 5th, 2003, 07:55 PM
sorry for the delay , nope it's not for controls , just unloading forms.
anyway , i have created a much better method for unloading all open forms:) ...

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 frm2 As New Form2()

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
frm2.Show()
Me.BringToFront()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objType As Type() = Reflection.Assembly.GetExecutingAssembly.GetTypes()
Dim x As Integer, i As Integer
Try
For x = LBound(objType) To UBound(objType)
If Not objType(x).Name = Me.Name Then '/// make sure you dont unload this form yet.
i = FindWindow(vbNullString, objType(x).Name)
PostMessage(i, CInt(&H10), vbNullString, vbNullString)
End If
Next
Catch ex As Exception
MessageBox.Show("Oops, the following error occured:" & ex.Message)
Finally
MyBase.Close() '/// now that all the other forms are closed , unload this one.
End Try
End Sub

dynamic_sysop
Aug 5th, 2003, 07:57 PM
Oops forgot to add the attachment sample code .zip :rolleyes:

biswajitdas
Nov 18th, 2003, 01:35 AM
to unload all aplication form in vb.net just use


application.exit()


das

dynamic_sysop
Nov 30th, 2003, 04:39 AM
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 :D