本文介绍了如何遍历 VSTO 加载项中的打开表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于 MS Project 的 VSTO 加载项,用于打开表单,其中数据与表单打开时处于活动状态的特定项目文件相关.可以打开与一个项目文件相关的一个表单,同时打开与第二个打开的项目文件相关的另一种不同的表单.

I have a VSTO add-in for MS Project that opens forms where the data is related to the specific project file that was active when the form was open. It is possible to open one form related to one project file, while having another different form open that is related to a second open project file.

当我关闭一个项目文件时,我想检查每个打开的表单,如果表单基本项目 ID 等于正在关闭的项目文件的项目 ID,则关闭它.我如何访问 vsto 应用程序的开放表单集合(或做一些等效的事情)?Application.OpenForms 对象似乎不存在于 vsto 世界中.

When I close a project file I would like to check each open form, and close it if the forms base project ID equals the project ID of the project file that is closing. How do I access the open forms collection of the vsto application (or do something equivalent)? The Application.OpenForms object doesn't appear to exist in the vsto world.

推荐答案

使用 Dictionary 对象,用于存储表单的实例以及文件名.每次创建表单时,将其添加到字典中,当项目关闭时,搜索字典以关闭相应的副本.

Use a Dictionary object to store an instance of the form along with the name of the file. Every time a form is created, add it to the dictionary and when the project is closed, search the dictionary to close the appropriate copy.

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)一起放在模块中.

Put this in the module with the application events (typically ThisAddIn).

Private Sub Application_ProjectBeforeClose(pj As MSProject.Project, ByRef Cancel As Boolean) Handles Application.ProjectBeforeClose
    ProjectForms(pj.Name).Close()
End Sub

这篇关于如何遍历 VSTO 加载项中的打开表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:44