Detecting when user clicks Red X button

  • Thread starter Thread starter ssg31415926
  • Start date Start date
S

ssg31415926

Is there anyway to detect when the user has clicked the red X button
to exit an application (as opposed to triggering some other event
which runs code which sends the Close() message to the form?

I know I can use the CloseReason property of FormClosingEventArgs but
that shows UserClosing whether they click the Red X or my Close button
(which sends Close() to the form).

Is there any way to determine that it was the red X button they
clicked?
 
You'll have to create a class that inherits from the Form class and override
WndProc like this:

Protected Overrides Sub WndProc(ByRef m As Message)
If ((m.Msg = WM_SYSCOMMAND) AndAlso _
(m.WParam.ToInt32 = SC_CLOSE)) Then

'Here is where you perform some action.
End If

MyBase.WndProc(m)
End Sub 'WndProc

where:

Public Const WM_SYSCOMMAND As Integer = &H112
Public Const SC_CLOSE As Integer = &HF060 'The Close menu item.

Tony
 
Is there any way to determine that it was the red X button they
clicked?

There's no red X button on ANY of my windows....

For reference, this is called the Close button.
 
You'll have to create a class that inherits from the Form class and override
WndProc like this:

Protected Overrides Sub WndProc(ByRef m As Message)
If ((m.Msg = WM_SYSCOMMAND) AndAlso _
(m.WParam.ToInt32 = SC_CLOSE)) Then

'Here is where you perform some action.
End If

MyBase.WndProc(m)
End Sub 'WndProc

where:

Public Const WM_SYSCOMMAND As Integer = &H112
Public Const SC_CLOSE As Integer = &HF060 'The Close menu item.

Tony

Thanks very much for your help.
 
Back
Top