WindowsFormsApplicationBase.OpenForms Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient une collection de tous les formulaires ouverts de l’application.
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
Valeur de propriété
Collection qui contient tous les formulaires ouverts de l’application.
Exemples
Cet exemple effectue une boucle sur les formulaires ouverts de l’application, sélectionne ceux qui sont directement accessibles par le thread actuel et affiche leurs titres dans un ListBox contrôle. Cet exemple nécessite que votre application Windows Forms dispose d’un formulaire nommé Form1 qui contient une zone de liste nommée 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
Cet exemple montre comment effectuer une boucle sur les formulaires ouverts de l’application et afficher leurs titres dans un ListBox contrôle.
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
Remarques
La My.Application.OpenForms propriété obtient une collection de tous les formulaires ouverts de l’application. Le comportement est identique à la Application.OpenForms propriété.
Note
La My.Application.OpenForms propriété retourne tous les formulaires ouverts, quel que soit le thread qui les a ouverts. Vous devez vérifier la InvokeRequired propriété de chaque formulaire avant de l’accéder ; sinon, elle peut lever une InvalidOperationException exception.
Disponibilité par type de projet
| Type de projet | Disponible |
|---|---|
| Windows Forms Application | Oui |
| Bibliothèque de classes | Non |
| Console Application | Non |
| Bibliothèque de contrôles Windows Forms | Non |
| Bibliothèque de contrôles web | Non |
| Service Windows | Non |
| Site Web | Non |