Solved

Closing child form in MDI application

Posted on 2006-07-11
3
682 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
[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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

705 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