我找不到 VB.Net 表单的任何属性来可靠地表明该表单已显示但仍未被处置。正如@smh 所说,令人失望。我的解决方案是按照@Hans Passant 的建议:“保留一个列表”,尽管我使用了一个集合。@Hans Passant 建议了另一个帖子,但它是 C# 帖子。Show
这是在 Visual Basic前后Close
或在 Visual Basic中管理表单的代码Dispose
:
在使用中,我SetOpenForm
在创建新表单、RemoveOpenForm
关闭表单(或单击表单上的接受)时调用。GetOpenForm
在这两个事件之间,可以使用& 表单的名称来检索表单及其所有数据。在一次只打开每个表单的一个实例的情况下是有效的。
Public Shared cOpenForms As Collection 'Place this at the top of your
'set of Forms, e.g. in your MyApp Class.
cOpenForms = New Collection 'Place this in the load sequence of MyApp.
Public Shared Sub SetOpenForm(NamedForm As Form)
'Saves an Open Form in the Collection of Open Forms
'Call this every time a New Form is created (if you want to revisit it).
MyApp.cOpenForms.Add(NamedForm)
End Sub
Public Shared Sub RemoveOpenForm(NamedForm As Form)
'Removes an Open Form in the Collection of Open Forms, if it is present.
'Silently ignores Forms that are not in the Collection.
'Call this every time a Form is finished with, Closed, Disposed.
If Not IsNothing(NamedForm) Then
If MyApp.cOpenForms.Contains(NamedForm.Name) Then
MyApp.cOpenForms.Remove(NamedForm.Name)
End If
End Sub
Public Shared Function GetOpenForm(FormName As String) As Form
'Retrieves a Form if it is in the Collection of Open Forms
'Call this to retrieve Form FormName; then check for IsNothing.
For Each f As Form In MyApp.cOpenForms
If f.Name = FormName Then
Return f
End If
Next
Return Nothing
End Function