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 フォーム アプリケーション はい
クラス ライブラリ いいえ
コンソール アプリケーション いいえ
Windows フォーム コントロール ライブラリ いいえ
Web コントロール ライブラリ いいえ
Windows サービス いいえ
Web サイト いいえ

適用対象

こちらもご覧ください