2016-08-02 28 views
1

我想以編程方式確定用戶是否在Visual Basic中使用表單時單擊了右上方的「x」按鈕。確定用戶何時單擊x-VBA

我曾嘗試:

Private Sub nameOfForm_Exit() 
    'code goes here 
End Sub 

而一直未果。任何幫助將非常感激。

+0

QueryClose事件:https://msdn.microsoft.com/en-us/library/office/gg278635.aspx(甚至會告訴您用戶是否按下了X或者窗體是以編程方式關閉還是...等等,以及允許您防止表單關閉,如果您願意的話) – Mikegrann

回答

5
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 
    MsgBox "Good bye" 

    '/ To prevent user from closing the form 
    '/ Set cancel to True 

    Cancel = True 

    MsgBox "You can't close me!" 
End Sub 
3

enter image description here 你不能創造自己的術語如 「退出」。你必須把它們放在上面的組合框中(稱爲事件)。由於您的事件與用戶窗體本身有關,因此您需要在左側組合框中選擇用戶窗體。您還將看到您的用戶窗體的所有控件,它們都有一組事件。

如前所述,QueryClose將引用右上角的紅色X.您也有Deactivate,這將發生在UserForm失去焦點時(如果它是ModeLess,意味着用戶可以單擊該表單後面的表單而不關閉它,這將觸發Deactivate)和Terminate,這發生在用戶窗體關閉後使用QueryClose 。

相關問題