Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
I have a program that, at times, has its window closed, but continues to run as a process in the background. I can use the Process class to find and kill the process, but what I'm looking for is a way to know for sure that the window is closed. How can I enumerate through all open forms to see if it exists. I have used the Windows API's in the past, but can it be done in .NET?

Codeless

...it just goes on and on.

Posted

There's several ways you can do this. Here's one (not necessarily the best, but worth consideration):

 

Put the following code in a Module.

Module FormsCollection 
Public Forms As New FormsCollectionClass() 
End Module 

Class FormsCollectionClass : Implements IEnumerable 
Public AllForms As New Collection() 

Sub Add(ByVal f As Form) 
AllForms.Add(f) 
End Sub 

Sub Remove(ByVal f As Form) 
Dim itemCount As Integer 
For itemCount = 1 To AllForms.Count 
If f Is AllForms.Item(itemCount) Then 
AllForms.Remove(itemCount) 
Exit For 
End If 
Next 
End Sub 

ReadOnly Property Item(ByVal index) As Form 
Get 
Return AllForms.Item(index) 
End Get 
End Property 

Overridable Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator 
Return AllForms.GetEnumerator 
End Function 
End Class 

 

In each Form in your project add:

Forms.Add(Me) 
'  'After the : "Add any initialization after the InitializeComponent" line

 

And again in each form, add:

Forms.Remove(Me) 
 'as the first line in the "Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) " code block

 

You can now test for how many open forms you have at any time with:

Forms.AllForms.Count
'  eg
If (Forms.AllForms.Count = 0) Then 
:

 

You can also enumerate through the forms collection and test any form property, including the Name if you wanted to test for a particular form's existence:

Dim frm As Form 
Dim TempStr As String 
For Each frm In Forms 
TempStr &=  f.Name & VBCRLF
Next

 

As I say, not the slickest way, but it's OK.

  • Leaders
Posted

i normally do it like this ...

   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

Posted
I have a program that' date=' at times, has its window closed, but continues to run as a process in the background. I can use the Process class to find and kill the process, but what I'm looking for is a way to know for sure that the window is closed. How can I enumerate through all open forms to see if it exists. I have used the Windows API's in the past, but can it be done in .NET?[/quote']

 

check out this library.

 

I believe there is a method, Win32Window.DesktopWindow.TopLevel, that may be what you need.

  • 10 months later...
Posted

Hey dynamic_sysop,

 

Your code interested me, but I can't determine what (or if) this line is supposed to do anything...

 

Dim hWnd As Integer = FindWindow(Nothing, typeForm.Name)

 

... because it always returns 0, no matter which forms are open or closed as I run the program. So all this does is loop thru all the forms included within the project? Thanks for your help.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...