DotNet · 2022年5月2日

My.Application.OpenForms 屬性

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

命名空間:  Microsoft.VisualBasic.ApplicationServices
組件:  Microsoft.VisualBasic (在 Microsoft.VisualBasic.dll 中)

'宣告Public ReadOnly Property OpenForms As FormCollection

屬性值

類型:System.Windows.Forms.FormCollection
FormCollection 物件,包含應用程式的所有開啟表單。

My.Application.OpenForms 屬性會取得所有應用程式之開啟表單的集合。 Application.OpenForms 屬性一樣。

注意事項注意事項

My.Application.OpenForms 屬性會傳回所有開啟表單,而不論開啟這些表單的執行緒為何。 InvokeRequired 屬性,否則可能擲回 InvalidOperationException 例外狀況。

依專案類型的可用性

專案類型

是否可用

Windows Form 應用程式

類別庫

沒有

主控台應用程式

沒有

Windows Form 控制項程式庫

沒有

Web 控制項程式庫

沒有

Windows 服務

沒有

網站

沒有

ListBox 控制項中顯示它們的標題。 Form1 的表單,其中包含名為 ListBox1 的清單方塊。

Private Sub GetOpenFormTitles()
    Dim formTitles As New Collection

    TryFor Each f As Form In My.Application.OpenForms
            If Not f.InvokeRequired Then' Can access the form directly.
                formTitles.Add(f.Text)
            End IfNextCatch 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

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

    Form1.ListBox1.DataSource = formTitles
End SubPrivate Delegate Function GetFormTitleDelegate(ByVal f As Form) As StringPrivate 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
        ElseReturn "Unresponsive thread"End IfEnd IfEnd Function
 

最新电影,电视剧,尽在午夜剧场

电影电视剧午夜不寂寞