Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef lParam As Integer) As Integer
Private Const WM_CLOSE As Integer = &H10
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim typeForm As Type
For Each typeForm In Reflection.Assembly.GetExecutingAssembly.GetTypes
If typeForm.IsSubclassOf(GetType(Form)) Then
Dim hWnd As Integer = FindWindow(Nothing, typeForm.Name)
If Not hWnd = 0 AndAlso Not hWnd = MyBase.Handle.ToInt32 Then '/// make sure we dont close this form ( yet )
SendMessage(hWnd, WM_CLOSE, 0, 0)
End If
End If
Next
'/// if you want to also close this form , you can now add --- MyBase.Close()
End Sub