关闭Microsoft Access时,如何在处理控件之前捕获CustomTaskPane的当前属性. [英] When Microsoft Access is shutting down, how can I catch the current properties of a CustomTaskPane before the controls are disposed..?

查看:165
本文介绍了关闭Microsoft Access时,如何在处理控件之前捕获CustomTaskPane的当前属性.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照Microsoft大师Andrew Whitechapel的指示为Microsoft Access创建了VSTO加载项

I've created a VSTO addin for Microsoft Access by following the directions by Microsoft guru Andrew Whitechapel here, and it's working nicely. But the addin has a CustomTaskPane, and I'm having an issue with it when Access is closing.

如果在Access关闭时打开了CustomTaskPane,则该插件应保存CustomTaskPane控件的属性.如果将此代码放在ThisAddIn_Shutdown()中,则会收到以下错误:

If the CustomTaskPane is open when Access closes, the addin should save the properties of the CustomTaskPane controls. If code for this is placed in ThisAddIn_Shutdown(), I receive the following error:

System.ObjectDisposedException: Cannot access a disposed object.
  at Microsoft.Office.Tools.CustomTaskPane.get_Control()
  at MyAddin.ThisAddIn.ThisAddIn_Shutdown(Object sender, EventArgs e) in C:\...\ThisAddIn.vb:line nn

我不确定这是否是CustomTaskPanes或Windows Forms控件的正常操作,还是因为VSTO不是为Access设计的.我想知道是否会发生这种情况,因为Access没有像Access&Application;这样的其他VSTO批准的应用程序那样具有Access.Application."OnClose"之类的应用程序级事件.字.

I'm not sure if this is the normal operation of CustomTaskPanes or Windows Forms controls, or if it's because VSTO isn't designed for Access. I'm wondering if it happens because Access doesn't have application-level events such as Access.Application."OnClose", as do the other VSTO-approved apps such as Excel & Word.

经过一些试验,我发现了对控件使用HandleDestroyed事件的变通办法,该事件发生在Dispose()之前,因此控件属性仍然可用.这有效:

After some experimentation I found a workaround by using the HandleDestroyed event for the controls, which occurs before Dispose(), and thus the control properties are still available. This works:

Private Sub TextBox1_HandleDestroyed(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles TextBox1.HandleDestroyed
    MsgBox(TextBox1.Text)
End Sub

有没有更好的方法..?解决方法使我感到紧张.

Is there a better way..? Workarounds make me nervous.

推荐答案

在跟踪事件之后,我意识到了自己问题的答案.焦点是usercontrol.designer.vb中的Dispose方法.但是,众所周知,不应直接修改设计者生成的代码,因为在对设计器中的用户控件进行任何后续更改之后,它可以并且将被刷新和覆盖.

In following the trail of events, I realized the answer to my own question. The focal point is the Dispose method in usercontrol.designer.vb. However, it is widely known designer-generated code shouldn't be directly modified, as it can and will be refreshed and over-written after any subsequent changes to the usercontrol in the designer.

除了...该规则并不完全适用于某些方法,例如Dispose. 请参阅此处.如果程序员随后将此类方法从usercontrol.designer.vb移到了usercontrol.vb,则设计人员将在这些方法中采用这些方法. usercontrol.vb,并且不会在usercontrol.designer.vb中重新生成它们.

Except...that rule doesn't completely apply to certain methods such as Dispose. See here. If the programmer subsequently moves such methods from usercontrol.designer.vb to usercontrol.vb, the designer will defer to them in usercontrol.vb and will not regenerate them in usercontrol.designer.vb.

因此,我们得出了答案:将Dispose方法移至usercontrol.vb,删除System.Diagnostics.DebuggerNonUserCode属性,然后添加必要的代码以保存控件属性,如下所示:

And so, we've arrived at the answer: move the Dispose method to usercontrol.vb, remove the System.Diagnostics.DebuggerNonUserCode attribute, and then add the necessary code to save the control properties, as shown below:

Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        SaveUserControlProperties()    <--- additional code added here
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try
End Sub

这篇关于关闭Microsoft Access时,如何在处理控件之前捕获CustomTaskPane的当前属性.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆