Detecting when user clicks Red X button

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?
 
G

Guest

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
 
J

Jeff Johnson

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.
 
S

ssg31415926

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top