WindowsFormsApplicationBase.OpenForms Proprietà

Definizione

Ottiene un insieme di tutti i form aperti dell'applicazione.

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

Valore della proprietà

Raccolta che contiene tutti i moduli aperti dell'applicazione.

Esempio

Questo esempio esegue il ciclo sui moduli aperti dell'applicazione, seleziona quelli direttamente accessibili dal thread corrente e visualizza i titoli in un ListBox controllo. In questo esempio è necessario che l'applicazione Windows Forms abbia un modulo denominato che contiene una casella di riepilogo denominata Form1ListBox1.

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

In questo esempio vengono scorrere i moduli aperti dell'applicazione e vengono visualizzati i titoli in un ListBox controllo.

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

Commenti

La My.Application.OpenForms proprietà ottiene una raccolta di tutti i moduli aperti dell'applicazione. Il comportamento è identico alla Application.OpenForms proprietà.

Nota

La My.Application.OpenForms proprietà restituisce tutti i moduli aperti, indipendentemente dal thread aperto. È consigliabile controllare la InvokeRequired proprietà di ogni modulo prima di accedervi. In caso contrario, potrebbe generare un'eccezione InvalidOperationException .

Disponibilità in base al tipo di progetto

Tipo di progetto Disponibile
Windows Forms Application
Libreria di classi No
Applicazione console No
Libreria di controllo Windows Form No
Libreria di controlli Web No
Servizio Windows No
Sito Web No

Si applica a

Vedi anche