Export (0) Print
Expand All

WindowsFormsApplicationBase.OpenForms Property

Gets a collection of all the application's open forms.

Namespace:  Microsoft.VisualBasic.ApplicationServices
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public FormCollection OpenForms { get; }

Property Value

Type: System.Windows.Forms.FormCollection
A FormCollection object that contains all of the application's open forms.

The My.Application.OpenForms property gets a collection of all the application's open forms. The behavior is identical to the Application.OpenForms property.

NoteNote

The My.Application.OpenForms property returns all open forms, regardless of which thread opened them. You should check the InvokeRequired property of each form before accessing it; otherwise, it might throw an InvalidOperationException exception.

Availability by Project Type

Project type

Available

Windows Forms Application

Yes

Class Library

No

Console Application

No

Windows Forms Control Library

No

Web Control Library

No

Windows Service

No

Web Site

No

This example loops over the application's open forms, selects the ones directly accessible by the current thread, and displays their titles in a ListBox control. This example requires that your Windows Forms application have a form named Form1 that contains a list box named ListBox1.

Private Sub GetOpenFormTitles()
    Dim formTitles As New Collection

    Try 
        For Each f As Form In My.Application.OpenForms
            If Not f.InvokeRequired Then 
                ' Can access the form directly.
                formTitles.Add(f.Text)
            End If 
        Next 
    Catch ex As Exception
        formTitles.Add("Error: " & ex.Message)
    End Try

    Form1.ListBox1.DataSource = formTitles
End Sub

This example loops over the application's open forms and displays their titles in a ListBox control.

Private Sub GetOpenFormTitles()
    Dim formTitles As New Collection

    Try 
        For Each f As Form In My.Application.OpenForms
            ' Use a thread-safe method to get all form titles.
            formTitles.Add(GetFormTitle(f))
        Next 
    Catch ex As Exception
        formTitles.Add("Error: " & ex.Message)
    End Try

    Form1.ListBox1.DataSource = formTitles
End Sub 

Private Delegate Function GetFormTitleDelegate(ByVal f As Form) As String 
Private Function GetFormTitle(ByVal f As Form) As String 
    ' Check if the form can be accessed from the current thread. 
    If Not f.InvokeRequired Then 
        ' Access the form directly. 
        Return f.Text
    Else 
        ' Marshal to the thread that owns the form.  
        Dim del As GetFormTitleDelegate = AddressOf GetFormTitle
        Dim param As Object() = {f}
        Dim result As System.IAsyncResult = f.BeginInvoke(del, param)
        ' Give the form's thread a chance process function.
        System.Threading.Thread.Sleep(10)
        ' Check the result. 
        If result.IsCompleted Then 
            ' Get the function's return value. 
            Return "Different thread: " & f.EndInvoke(result).ToString
        Else 
            Return "Unresponsive thread" 
        End If 
    End If 
End Function

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

  • UIPermission 

    Controls the permissions that are related to user interfaces and the Clipboard. Associated enumeration: AllWindows.

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft