Share via


WindowsFormsApplicationBase.OpenForms 屬性

定義

取得應用程式之所有開啟表單的集合。

public:
 property System::Windows::Forms::FormCollection ^ OpenForms { System::Windows::Forms::FormCollection ^ get(); };
public System.Windows.Forms.FormCollection OpenForms { get; }
member this.OpenForms : System.Windows.Forms.FormCollection
Public ReadOnly Property OpenForms As FormCollection

屬性值

集合,包含所有應用程式的開啟表單。

範例

此範例會在應用程式的開啟窗體上循環、選取目前線程直接存取的表單,並在控件中 ListBox 顯示其標題。 此範例需要您的 Windows Forms 應用程式具有名為 Form1 的表單,其中包含名為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

這個範例會在應用程式的開啟表單上迴圈,並在控件中 ListBox 顯示其標題。

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(f As Form) As String
Private Function GetFormTitle(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

備註

屬性 My.Application.OpenForms 會取得所有應用程式開啟表單的集合。 行為與屬性相同 Application.OpenForms

注意

屬性 My.Application.OpenForms 會傳回所有開啟的窗體,而不論開啟的線程為何。 您應該先檢查 InvokeRequired 每個表單的屬性,再存取它,否則可能會擲回 InvalidOperationException 例外狀況。

依專案類型的可用性

專案類型 可用
Windows Forms 應用程式
類別庫
主控台應用程式
Windows Form 控制項程式庫
Web 控制項程式庫
Windows 服務
網站

適用於

另請參閱