vb.net:区分me.close()和x-button
vb.net: Distinguish between me.close() and x-button
如何区分用户明确按下 x 按钮(或按下 ALT F4)以关闭表单的事件和所有其他以编程方式关闭表单的方法 (me.close( ), ETC)。
我已经想通了,这可以使用发件人对象来完成,但我真的不明白。有人可以举个例子给我解释一下吗?
非常感谢大家。
就这么简单。 :)
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
e.Cancel = True
End Sub
这将阻止 X
按钮关闭表单以及 :)。问题是,如果您这样做,您也无法使用 Me.Close
关闭表单,要解决此问题,只需删除 EventHandler
:)
RemoveHandler Me.Closing, AddressOf Form1_FormClosing
'Now close the form
Me.Close()
class FormClosingEventArgs 事件参数有一个枚举来说明表单关闭的原因。
https://msdn.microsoft.com/en-us/library/system.windows.forms.closereason(v=vs.110).aspx
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.Closing
If Not appClosing AndAlso e.CloseReason = System.Windows.Forms.CloseReason.UserClosing Then
' DO WHATEVER CODE YOU WANT IN HERE
' LIKE SETTING E.CANCEL TO TRUE
End If
End Sub
' You can also set a close appClosing local variable at the class level as a boolean, whenever you call closing in code, assign the variable.
appClosing = True
Me.Close();
如何区分用户明确按下 x 按钮(或按下 ALT F4)以关闭表单的事件和所有其他以编程方式关闭表单的方法 (me.close( ), ETC)。 我已经想通了,这可以使用发件人对象来完成,但我真的不明白。有人可以举个例子给我解释一下吗? 非常感谢大家。
就这么简单。 :)
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
e.Cancel = True
End Sub
这将阻止 X
按钮关闭表单以及 :)。问题是,如果您这样做,您也无法使用 Me.Close
关闭表单,要解决此问题,只需删除 EventHandler
:)
RemoveHandler Me.Closing, AddressOf Form1_FormClosing
'Now close the form
Me.Close()
class FormClosingEventArgs 事件参数有一个枚举来说明表单关闭的原因。
https://msdn.microsoft.com/en-us/library/system.windows.forms.closereason(v=vs.110).aspx
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.Closing
If Not appClosing AndAlso e.CloseReason = System.Windows.Forms.CloseReason.UserClosing Then
' DO WHATEVER CODE YOU WANT IN HERE
' LIKE SETTING E.CANCEL TO TRUE
End If
End Sub
' You can also set a close appClosing local variable at the class level as a boolean, whenever you call closing in code, assign the variable.
appClosing = True
Me.Close();