Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2007

Tip: Looking for answers? Try searching our database.

intercept form closing

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alexander Stetsenko - 04 Jun 2007 10:41 GMT
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.
Manish Bafna - 04 Jun 2007 11:21 GMT
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);
}
}
Signature

Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

> Hi.
>
[quoted text clipped - 12 lines]
> Thanks.
> Alexander.
Alexander Stetsenko - 04 Jun 2007 11:49 GMT
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?

> Hi,
> You can give users the option to change their minds by handling the
[quoted text clipped - 27 lines]
>> Thanks.
>> Alexander.
-pb- - 04 Jun 2007 12:23 GMT
On 4 Jun, 11:49, "Alexander Stetsenko" <alexan...@NOSPAMcosmoset.com>
wrote:
> It will work if FormClosing event fire before Closing (and before closing
> all child windows)
[quoted text clipped - 41 lines]
>
> - 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
Mehdi - 04 Jun 2007 12:10 GMT
> What I need is to intercept MDI Parent form closing.
>
[quoted text clipped - 7 lines]
>
> 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?
Alexander Stetsenko - 04 Jun 2007 12:49 GMT
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.

> Hi.
>
[quoted text clipped - 12 lines]
> Thanks.
> Alexander.
Peter Duniho - 04 Jun 2007 17:12 GMT
> 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
Alexander Stetsenko - 05 Jun 2007 06:57 GMT
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.
Peter Duniho - 05 Jun 2007 09:09 GMT
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.