Solved

How do you trap the close button on an MDI child form?

Posted on 2008-06-20
2
817 Views
Last Modified: 2013-12-03
I have a custom use application I am developing, but I'm having a problem with some child forms.  I have one child form (an output console) that needs to be opened to see the debugging output.  I have no problem getting the form to open, minimize, etc.  but, I cannot keep it from releasing it's resources when you click the close (X) button.  I DO NOT want to disable the button.  What I have done is setup an event handler for when the form closes.  Instead of closing, it hides the window.   However, as soon as the window is hidden once, if you try to show (form.show() or form.visible = true;) it errors out and the application locks up.  The error message from VS08 is: System.ObjectDisposedException was unhandled.  The object that was unhandled is the form I want to show.  I'm guessing what is happening is it is getting disposed before the code comes in from my event handler (e.cancel():)  So, I'm guessing my only option is to somehow trap the close button and never initiate a close to being with.  If I could force it to only hide, that should be perfect.  I've searched for days on end and cannot find any solutions other than disabling the close button (again, CAN NOT do this) or handling the closing event like I'm already doing.

Below is the code for my event hanlder:

private void consoleForm_FormClosing(object sender, FormClosingEventArgs e)
        {
                e.Cancel = true;
                this.Hide();
        }

Open in new window

0
Comment
Question by:Journer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
Bruce_1975 earned 125 total points
ID: 21831954
You have to override the event handler ;o)

Regards,
Bruce
protected override void OnClosing(CancelEventArgs e)
{
    e.Cancel = true;
    this.Hide();
}

Open in new window

0
 
LVL 2

Author Comment

by:Journer
ID: 21832105
you rock!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question