How to intercept click on title bar X

S

Serge Wautier

Env.: CF 3.5, WM6

Hi All,

I'd like to know when user clicks the X on the title bar of my form.
I thought the Deactivate event would be a good place but it isn't good
enough: The event is fired in other circumstances as well such as opening a
program from the start menu.

I tried this unfortunately to no avail (hoping that Visible would be turned
off only when user clicks the X. Might be a stupid assumption since we have
only one visible form system-wide). Anyway the 'funny' thing is that Visible
is true in all cases when the event handler is called.

private void MyForm_Deactivate(object sender, EventArgs e)
{
if (!Visible)
{
System.Diagnostics.Debug.WriteLine("hello");
}
}

TIA for your help.

Serge.
 
S

Serge Wautier

No, these events are not fired since the form is not closed since they're
displayed using Show(), not ShowDialog().
The form is hidden, not closed.

Serge.
 
S

Serge Wautier

After some investigation, I discovered that the only event always triggered
is Resize!
But it's hard to say from within OnResize what the source of the event is.
OK, I guess it means my design is incorrect. I'll change it not to have to
rely on this event.

Cheers,

Serge.
 
S

Simon Hart [MVP]

I don't think there is anyway to trap this as pressing the X is the same as
setting the position of the form to the back of the z order. Change your form
to OK: MinimizeBox = false; which will enable you to catch Closing event. But
this will close the form.
 
S

Serge Wautier

Simon,

Thanks for your help. That's interesting light shed on my problem.

Serge.
 

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