WindowsFormsApplicationBase.OpenForms 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取应用程序中所有打开的窗体的集合。
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 服务 | 否 |
| 网站 | 否 |