使用
Dictionary
对象来存储窗体的实例以及文件名。每次创建表单时,将其添加到词典中,当项目关闭时,搜索词典以关闭相应副本。
Friend ProjectForms As New Dictionary(Of String, MyForm)
Friend Sub ShowForm()
Dim f As New MyForm
Try
ProjectForms.Add(ProjApp.ActiveProject.Name, f)
Catch AlreadyInTheDictionary As Exception
' do nothing, it's already in the dictionary
End Try
f.Show()
End Sub
将其与应用程序事件一起放入模块中(通常是ThisAddIn)。
Private Sub Application_ProjectBeforeClose(pj As MSProject.Project, ByRef Cancel As Boolean) Handles Application.ProjectBeforeClose
ProjectForms(pj.Name).Close()
End Sub