WindowsFormsApplicationBase.OpenForms Eigenschaft
Definition
Ruft eine Auflistung aller geöffneten Formulare der Anwendung ab.Gets a collection of all the application's open forms.
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
Eigenschaftswert
Eine Auflistung, die alle geöffneten Formulare der Anwendung enthält.A collection that contains all of the application's open forms.
Beispiele
In diesem Beispiel werden die geöffneten Formulare der Anwendung durchlaufen, diejenigen, die direkt für den aktuellen Thread zugänglich sind, ausgewählt und ihre Titel in einem-Steuerelement angezeigt ListBox .This example loops over the application's open forms, selects the ones directly accessible by the current thread, and displays their titles in a ListBox control. Für dieses Beispiel ist es erforderlich, dass die Windows Forms-Anwendung ein Formular namens Form1
enthält, das ein Listenfeld mit dem Namen enthält ListBox1
This example requires that your Windows Forms application have a form named Form1
that contains a list box named 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
In diesem Beispiel werden die geöffneten Formulare der Anwendung durchlaufen, und ihre Titel werden in einem-Steuerelement angezeigt ListBox .This example loops over the application's open forms and displays their titles in a ListBox control.
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
Hinweise
Die My.Application.OpenForms
-Eigenschaft ruft eine Auflistung aller geöffneten Formulare der Anwendung ab.The My.Application.OpenForms
property gets a collection of all the application's open forms. Das Verhalten ist identisch mit der- Application.OpenForms Eigenschaft.The behavior is identical to the Application.OpenForms property.
Hinweis
Die- My.Application.OpenForms
Eigenschaft gibt alle geöffneten Formulare zurück, unabhängig davon, welcher Thread Sie geöffnet hat.The My.Application.OpenForms
property returns all open forms, regardless of which thread opened them. Sie sollten die- InvokeRequired Eigenschaft jedes Formulars vor dem Zugriff überprüfen; andernfalls wird möglicherweise eine InvalidOperationException Ausnahme ausgelöst.You should check the InvokeRequired property of each form before accessing it; otherwise, it might throw an InvalidOperationException exception.
Verfügbarkeit nach ProjekttypAvailability by Project Type
ProjekttypProject type | VerfügbarAvailable |
---|---|
Windows Forms-AnwendungWindows Forms Application | JaYes |
KlassenbibliothekClass Library | NeinNo |
KonsolenanwendungConsole Application | NeinNo |
Windows Forms-SteuerelementbibliothekWindows Forms Control Library | NeinNo |
WebsteuerelementbibliothekWeb Control Library | NeinNo |
Windows-DienstWindows Service | NeinNo |
WebsiteWeb Site | NeinNo |