Solved

Closing child form in MDI application

Posted on 2006-07-11
3
670 Views
Last Modified: 2012-05-05
Hi all,

I have a MDI application, in child form class I write event handler to handle FormClosing event. In the event handler I show a message box to ask user to save their changes to the document. I write code to do the task as follow. If user selects Yes the document will be save and the form will be closed, if user selects No then form will be close without saving document, and if user select Cancel the Cancel property of FormClosingEventArgs will be set to true and do nothing. But when I run my application, when message is promted I select No in some form and select Cancel at the message of last form. If it is correct then there is one child form in my application but in my application, all child form still remaining and not closed.

Please tell me why and let me know the way to solve this problem.
thanks and best regard.
0
Comment
Question by:ncsvietnam
3 Comments
 
LVL 2

Accepted Solution

by:
Anandavally_P earned 125 total points
ID: 17089304
Try this,

This is the closing event of the child form.

 private void Form2_FormClosing(object sender, FormClosingEventArgs e)
 {
      DialogResult result = MessageBox.Show(this, "Do you want to close", "Confirm",
                                                                 MessageBoxButtons.YesNoCancel);
      // If the result is yes then save the form and the form is closed
       if (result == DialogResult.Yes)
       {
            MessageBox.Show("save the form");                
       }
       // If the result is no dont save the form and the form is closed
       else if (result == DialogResult.No)
       {
            MessageBox.Show("Do not save the form");                
       }
      // If the result is cancel then set the argument to cancel value to true
       else if (result == DialogResult.Cancel)
       {
            e.Cancel = true;
        }                  
 }

I think you would have called the function this.Close() after saving the form. It will again call the form closing event and eventually you will end up in keeping the form open.

Regards,
Anandavally P
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now