共用方式為


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 表單應用程式必須有一個名為 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 表單應用程式 是的
類別庫 No
主控台應用程式 No
Windows 表單控制函式庫 No
網頁控制函式庫 No
Windows 服務 No
網站 No

適用於

另請參閱