intercept form closing

  • Thread starter Alexander Stetsenko
  • Start date
A

Alexander Stetsenko

Hi.

What I need is to intercept MDI Parent form closing.

As for now, MDI Parent form closing event is rased only after all child
forms are closed.
And I can't cancel this operation. If user had accidentaly clicked form X
close button
I want to ask him if hi realy want to close thw application. BEFORE all
opened child windows
are closed.

How can I do this?

Thanks.
Alexander.
 
G

Guest

Hi,
You can give users the option to change their minds by handling the
FormClosing event and setting FormClosingEventArgs.Cancel to true or false as
appropriate:
//if we set e.cancel to true means it will not close the application
void Form1_FormClosing(object sender, FormClosingEventArgs e) {
if( e.CloseReason == CloseReason.UserClosing){
DialogResult result = MessageBox.Show(
"Abort your application?", "Application In Progress",
MessageBoxButtons.YesNo);
e.Cancel = (result == DialogResult.No);
}
}
 
A

Alexander Stetsenko

It will work if FormClosing event fire before Closing (and before closing
all child windows)
But if is fired after Closing event. So it can't help me. All child windows
at that moment are closed :(

Other solutions?
 
M

Mehdi

What I need is to intercept MDI Parent form closing.

As for now, MDI Parent form closing event is rased only after all child
forms are closed.
And I can't cancel this operation. If user had accidentaly clicked form X
close button
I want to ask him if hi realy want to close thw application. BEFORE all
opened child windows
are closed.

How can I do this?

It's been a long time since I've worked with MDI windows but what about
overriding the WndProc method and intercepting whatever message is posted
when the user clicks on the close button there?
 
P

-pb-

It will work if FormClosing event fire before Closing (and before closing
all child windows)
But if is fired after Closing event. So it can't help me. All child windows
at that moment are closed :(

Other solutions?







- Show quoted text -

try following code. It works for me. No matter whether how many child
windows are open. Trap FormClosing event and not FormClosed event. You
can put additional loginc in If to check if any child window is
visible ot not and if not then don't ask any question.

private void MDIParent1_FormClosing(object sender,
FormClosingEventArgs e)
{
if (MessageBox.Show("Close?",
AppDomain.CurrentDomain.ToString(), MessageBoxButtons.YesNo) ==
DialogResult.No)
{
e.Cancel = true;
}
}

This is definatly working for me as we had recently deliver one of our
project to client with simillar feature
 
A

Alexander Stetsenko

Thanks all!

Your solutions works fine.

I found the problem. It is Janus MdiTab Manager. Without it the sequence of
events is correct and
FormClosing is works fine. But the I activate it all child forms closes
before I enter any of form closing event handlers.

Thanks again.
 
P

Peter Duniho

I found the problem. It is Janus MdiTab Manager. Without it the sequence
of events is correct and
FormClosing is works fine. But the I activate it all child forms closes
before I enter any of form closing event handlers.

Is the Janus component part of the inheritance chain for your form? Or is
it simply adding an event handler to the FormClosing event? Either way,
it seems to me that this is an example of why it's a good idea to override
the OnFormClosing() method in your form, rather than to subscribe to its
own event. If you override the OnFormClosing() method, you can ensure
that your code is executed first, before any base class and before any
event handlers. You can even avoid calling any of that other code if you
don't need to (if canceling the close, for example).

Pete
 
A

Alexander Stetsenko

It's a component, placed on form.
I've already tried to override OnFormClosing, but with no success.
It seems like this component handling Windows messages directly.
 
P

Peter Duniho

It's a component, placed on form.
I've already tried to override OnFormClosing, but with no success.
It seems like this component handling Windows messages directly.

I don't see how it can do that, unless it's doing some low-level message
hooks or something. That's certainly not the sort of behavior I'd expect
from a .NET Forms component. Sounds like a good reason not to use that
component if it can be helped. :)

Pete
 

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