How to know user has clicked “X” or the “Close” button?

前端 未结 12 1020
野性不改
野性不改 2020-11-27 12:19

In MSDN I found CloseReason.UserClosing to know that the user had decided to close the form but I guess it is the same for both clicking the X button or clickin

12条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 12:59

    I agree with the DialogResult-Solution as the more straight forward one.

    In VB.NET however, typecast is required to get the CloseReason-Property

        Private Sub MyForm_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
    
            Dim eCast As System.Windows.Forms.FormClosingEventArgs
            eCast = TryCast(e, System.Windows.Forms.FormClosingEventArgs)
            If eCast.CloseReason = Windows.Forms.CloseReason.None Then
                MsgBox("Button Pressed")
            Else
                MsgBox("ALT+F4 or [x] or other reason")
            End If
    
        End Sub
    

提交回复
热议问题