Handle the "X" button

Y

yhlove

Hi

I would like to know how can I handle the event that the user press
the red button "X" (The button which suppose to close the program) ?
I tried to double click on it (in the form editor) but nothing
happened...


Thanks
Yhlove
 
O

\(O\)enone

I would like to know how can I handle the event that the user press
the red button "X" (The button which suppose to close the program) ?
I tried to double click on it (in the form editor) but nothing
happened...

Try handling the FormClosing event of the form if you want to know the user
closed the window but before it actually closes (also giving you an
opportunity to cancel the closure if you wish), and/or the FormClosed event
(which fires once it actually has closed).

HTH,
 
C

Cor Ligthert [MVP]

In addition, when you then want to cancel it, set in that event
e.cancel=true

Cor
 
R

rowe_newsgroups

Hi

I would like to know how can I handle the event that the user press
the red button "X" (The button which suppose to close the program) ?
I tried to double click on it (in the form editor) but nothing
happened...

Thanks
Yhlove

Just in case the other mentioned events don't do what you need/want,
here's a snippet from Herfried Wagner that can be used to trap the
windows messages that correspond to the 3 buttons on the form (close,
minimize, restore down)

Oh, please no one yell at me for posting this here, I realize in 99%
of situations the FormClosing/FormClosed events will be more than
sufficient. This is mainly for the benefit of people who might be
searching the archives for "Handle X button" that need to use a
wndproc override (you know, the other 1%)

Thanks,

Seth Rowe

Is there a way I can get into a form's close/minimize/maximize events when
those buttons (the 3 small squared button in the upper right corner of a
form) are clicked?

\\\
Private Const WM_SYSCOMMAND As Int32 = &H112

Private Const SC_MAXIMIZE As Int32 = &HF030
Private Const SC_MINIMIZE As Int32 = &HF020
Private Const SC_RESTORE As Int32 = &HF120
Private Const SC_CLOSE As Int32 = &HF060

Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_SYSCOMMAND Then
Select Case m.WParam.ToInt32()
Case SC_MAXIMIZE
Debug.WriteLine("Form gets maximized.")
Case SC_MINIMIZE
Debug.WriteLine("Form gets minimized.")
Case SC_RESTORE
Debug.WriteLine("Form gets restored.")
Case SC_CLOSE
Debug.WriteLine("Form gets closed.")
End Select
End If
MyBase.WndProc(m)
End Sub
///
 
R

RobinS

rowe_newsgroups said:
Just in case the other mentioned events don't do what you need/want,
here's a snippet from Herfried Wagner that can be used to trap the
windows messages that correspond to the 3 buttons on the form (close,
minimize, restore down)

Oh, please no one yell at me for posting this here, I realize in 99%
of situations the FormClosing/FormClosed events will be more than
sufficient. This is mainly for the benefit of people who might be
searching the archives for "Handle X button" that need to use a
wndproc override (you know, the other 1%)

Thanks,

Seth Rowe



\\\
Private Const WM_SYSCOMMAND As Int32 = &H112

Private Const SC_MAXIMIZE As Int32 = &HF030
Private Const SC_MINIMIZE As Int32 = &HF020
Private Const SC_RESTORE As Int32 = &HF120
Private Const SC_CLOSE As Int32 = &HF060

Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_SYSCOMMAND Then
Select Case m.WParam.ToInt32()
Case SC_MAXIMIZE
Debug.WriteLine("Form gets maximized.")
Case SC_MINIMIZE
Debug.WriteLine("Form gets minimized.")
Case SC_RESTORE
Debug.WriteLine("Form gets restored.")
Case SC_CLOSE
Debug.WriteLine("Form gets closed.")
End Select
End If
MyBase.WndProc(m)
End Sub
///

Thanks for posting this. It's really useful information.

Robin S.
 

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