クラスモジュール(たとえばTestClass)で次のようにインスタンス化されているUserForm、xFormがあります。
'TestClass
Dim Form as New xForm
Private WithEvents EvForm as MSForms.UserForm
Set EvForm = Form
xForm自体のクラスモジュールには、フォームが実際に閉じた場合にのみ、フォームを閉じるときに実行する必要のあるコードがあります。
'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でも扱われ、フォームが閉じないようにすることができます。
'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クラスモジュールでTestClassに設定されたCancel=Trueをテストするにはどうすればよいですか?言い換えると、TestClassでCancelがTrueに設定されている場合、xFormクラスモジュールでクリーンアップコードを実行してはなりません。どうすればそれを達成できますか?
これまで、xFormクラス(My_QueryClose?)に別のイベントを実装して、QueryCloseイベントで発生させることを考えていました。コードビハインドフォームの外では、My_QueryCloseイベントのみを処理するため、何が起こっているかを完全に制御します。これは実行可能/より良いアプローチですか?