VBA:WithEvents难题

编程入门 行业动态 更新时间:2024-10-05 15:28:49
本文介绍了VBA:WithEvents难题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个UserForm, xForm ,正在一个类模块中被实例化(让我们说 TestClass )为:

I have a UserForm, xForm, that is being instantiated in a class module (let's say TestClass) as:

'TestClass Dim Form as New xForm Private WithEvents EvForm as MSForms.UserForm Set EvForm = Form

在xForm本身的类模块中,我有一些必须在表单关闭时执行的代码,只有当表单实际关闭时: / p>

At the class module of the xForm itself I have some code that must be executed on Form Closing, ONLY if the form actually closes:

'xForm class module Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 'Do some cleanup, otherwise the app would hang 'If not closing, don't cleanup anything, otherwise the app would hang End Sub

QueryClose事件也在TestClass中处理,可以避免关闭窗体:

The QueryClose event is also treated in TestClass, and could avoid the form from closing:

'TestClass Private Sub EvForm_QueryClose(Cancel As Integer, CloseMode As Integer) 'Verify if closing is allowed based on User Control values Cancel = Not ClosingIsAllowed '<-- Pseudocode on the right side of "=" End Sub

如何在xForm类模块中测试Cancel = True,在TestClass中设置? 让我们重新表述:如果TestClass中的Cancel被设置为True,我不能在xForm类模块中执行清除代码。如何才能实现?

How can I test for Cancel = True, set in TestClass, in the xForm class module? Let's rephrase it: If Cancel is set to True in TestClass, I must not do the cleanup code in the xForm class module. How can I accomplish that?

到目前为止,我已经考虑在xForm类(My_QueryClose?)中实现另一个事件,并将其提交到QueryClose事件。在代码后面我将只处理My_QueryClose事件,所以完全控制发生了什么。这是一个可行/更好的方法吗?

Until now, I have thought off of implementing another event in the xForm class (My_QueryClose?) and raise it on the QueryClose event. Outside the Code Behind Form I would deal only with the My_QueryClose event, so taking full control over what is happening. Is this a viable/better approach?

推荐答案

解决另一个事件的工作

代码不如我所期望的那样,尽管它不像我希望的那样整齐。

The code bellow do what I was expecting, although it is not as neat as I wish it could be.

在 UserForm1 代码:

'***** UserForm1 Public Event MyQueryClose(ByRef Cancel As Integer, ByRef CloseMode As Integer, ByRef Status As String) Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) Dim Status As String Cancel = True Status = "QueryClose" Debug.Print "Entered QueryClose" Debug.Print "Cancel = " & Cancel Debug.Print "Status = " & Status Debug.Print "Just before raising MyQueryClose" RaiseEvent MyQueryClose(Cancel, CloseMode, Status) Debug.Print "Just got back from MyQueryClose" Debug.Print "Cancel = " & Cancel Debug.Print "Status = " & Status End Sub

在 Class1 代码中: p>

In the Class1 code:

'***** Class1 Dim UserForm As New UserForm1 Private WithEvents UF As UserForm1 Sub DoIt() Set UF = UserForm UserForm.Show End Sub Private Sub UF_MyQueryClose(Cancel As Integer, CloseMode As Integer, Status As String) Debug.Print "Just entered MyQueryClose" Cancel = False Status = "MY QueryClose" End Sub

在基本模块中,测试Class:

In a basic module, to test the Class:

'***** Basic module Sub TestClass() Dim C As New Class1 C.DoIt End Sub

这是最终结果(调试窗口):

And here's the end result (debug window):

TestClass Entered QueryClose Cancel = -1 Status = QueryClose Just before raising MyQueryClose Just entered MyQueryClose Just got back from MyQueryClose Cancel = 0 Status = MY QueryClose

更多推荐

VBA:WithEvents难题

本文发布于:2023-11-29 15:17:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646771.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:难题   VBA   WithEvents

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!