Hook the Form's X button

T

Thomas

Hello CF Programmers

I have a question about getting an event when the Form's X button is
pressed.

Due to performance issues, I want to leave the Forms open and used
them again when needed. So I set the MinimizeBox to true. To save data
changes made in the Form, I need to know when the Form's X button is
pressed. Sadly there is no standard event in Form class.

Can somebody tell me how to get notified when the X Button is pressed?

I cannot use the Deactivate event because its also fired when form is
send to background.
I tried to P/Invoke GetWindow to determine if form is sent to
background, but its not working perfectly in all cases.
Can I P/Invoke RegisterHotKey to hook the X Button?

I appreciate your help


Greets,

Thomas
 
T

Thomas

The Closing or Closed event is not fired when MinimizeBox is set to
true, its only fired when form is shown modal.

This is exactly the point:
I'm looking for the event on "X" Button (Modeless) not "OK" Button
(Modal).


Any suggestions?

Thanks,

Thomas
 
T

Thomas

Hi

The deactivated event is fired whenever the form is sent to
background. So it is also fired when the user opens an other programm
or go to "Today" screen. But I want to get notified only if the user
presses the "X" button explicit.

My intention is:

When the user presses "X", it means that he has finished work with
this form and I need to save the data.
When the form is deactivated, it means that the application has been
sent to background (i.e. because there is an incoming call or the user
opens another program) and he wants to continue his work on this Form
later. Then I must not save the data.


Please help,

Thomas
 
T

Thomas

If MSDN is not wrong, the IMessageFilter Interface and WndProc is not
supported in Compact Framework.

Is there a way to do it in CF?


Greets,

Thomas
 
T

Thomas

Hello Nino

Thank you for your reply.

You're right that WndProc is supported in CF. But after testing I
figured out that WndProc does only receive messages that I generate
myself. So I'm at back to square one.

Anyway, for all those who are not irritated by my persist posts, I
will ask again:

Due to performance issues, I've made a Forms-Pool where the Form is
instantiated once and can be used again and again. Whenever I need a
form, I get it from the FormsPool, load it with new data and display
it. This is done with MinimizeBox set to true and Form.Show(). Becuase
these Forms are nod modal, the Closing/Closed events are not fired.

When I display a Form and the User presses the "X" button, it means
that he has finished work with this form and I need to save the data.
I want to popup a MessageBox like "You've made changes. Do you want to
save them?" befoe he manually hides the Form. I cannot use the
deactivated event, because this is also fired when the user opens an
other application. In this case it wont be nice when he changes to
Telephone and the MessageBox pops up, even when he did not press the
"X" button himself.

The event I want to catch is exactly when the user presses the "X"
button. The Deactivated event does not make the difference if the user
send the Form to background himself ("X" button) or if the form is
send to background by opening another Application on Top.

Does anybody know how I can get the "X" button event?
Or is this idea with FormsPool and reusing them bad design? I'm
working on this problem for days now, and start doubting in a proper
solution...

Thanks for all replies,

Thomas
 
T

Thomas

Hello Nino

Thank you for your reply.

Its true that CF does support WndProc, but after I tried it, I figured
out that I can only receive messages that I generated myself. So I
back at square one again.

For all those people why are not irritaded in my persistand behavior,
I will ask again:

Due to performance issues, I've made a FormsPool where the Forms are
instantiated once and can be used again and again. Whenever I need to
display a Form, I get it from FormsPool, update its content and
display it. This is done by setting MinimizeBox to true and
Form.Show(). Because of this Modeless Forms, the Closing/Closed event
is useless because they are not fired.

When the user presses "X" on a Form, after he edited some data, it
means that he has finished his work on this form. After that, I plan
to display a MessageBox like "You have made changes. Do you want to
save them?"

I cannot use the deactivated event because it is also fired when the
Form is sent to background by opening another application. I.e. it
wont be nice when the User opens the Calculator and then the
MessageBox pops up.

So I exactly want to catch when the User presses the "X" button
manually, but not when the Form is deactivated in general.

Does anybody know how to achieve this goal?
I am working on this problem for days now, and slowly start doubting
in the benefit of the FormsPool. Is this bad design for CF?

I'm happy for any comments and help!

Greetings,

Thomas
 
T

Thomas

Hello Chris

Thanks for your help.

A small tesprogramm has brought me to the conclusion, that there is no
way to catch the "X" button.

The IMessageFilter from OpenNETCF shows only the following messages
while a Form is sent to background:
WM_CANCELMODE 0x001F

There is no difference between explicit press the "X" button and
implicit sending the form to background.
So I will redesing my architecture....


Greets,

Thomas
 

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