I would provide a solution based on interfaces. This way will be easy for you to have an uniform way of managing if the application can be closed or not. With the following implementation the parent-form is in charge of asking the child-window if is ready to be closed, the child does whatever actions has to be done and replies to main window.
Let suppose I have the interface IManagedForm
:
interface IManagedForm
{
bool CanIBeClosed(Object someParams);
}
Both forms (Form1
and ChildForm
) would implement it.
Note that for this example I'm instantiating the ChildForm
in this way:
ChildForm cf = new ChildForm() { Owner = this, Name = "ChildForm" };
cf.Show();
Here comes first the implementation of the interface by Form1
:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
object someArgsInterestingForTheMethod = new object();
e.Cancel = !((IManagedForm)this).CanIBeClosed(someArgsInterestingForTheMethod);
}
// Ask the ChildForm it is done. If not the user should not leave the application.
public bool CanIBeClosed(object someParams)
{
bool isOKforClosing = true;
var cf = this.Controls["ChildForm"] as IManagedForm;
if (cf != null)
{
isOKforClosing = cf.CanIBeClosed(someParams);
if (!isOKforClosing)
{
MessageBox.Show("ChildForm does not allow me to close.", "Form1", MessageBoxButtons.OK);
}
}
return isOKforClosing;
}
And finally your ChildForm
implementation of the interface would look like this:
private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
{
object someArgsInterestingForTheMethod = new object();
e.Cancel = !((IManagedForm)this).CanIBeClosed(someArgsInterestingForTheMethod);
}
public bool CanIBeClosed(object someParams)
{
// This flag would control if this window has not pending changes.
bool meetConditions = ValidateClosingConditions(someParams);
// If there were pending changes, but the user decided to not discard
// them an proceed saving, this flag says to the parent that this form
// is done, therefore is ready to be closed.
bool iAmReadyToBeClosed = true;
// There are unsaved changed. Ask the user what to do.
if (!meetConditions)
{
// YES => OK Save pending changes and exit.
// NO => Do not save pending changes and exit.
// CANCEL => Cancel closing, just do nothing.
switch (MessageBox.Show("Save changes before exit?", "MyChildForm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
{
case DialogResult.Yes:
// Store data and leave...
iAmReadyToBeClosed = true;
break;
case DialogResult.No:
// Do not store data, just leave...
iAmReadyToBeClosed = true;
break;
case DialogResult.Cancel:
// Do not leave...
iAmReadyToBeClosed = false;
break;
}
}
return iAmReadyToBeClosed;
}
// This is just a dummy method just for testing
public bool ValidateClosingConditions(object someParams)
{
Random rnd = new Random();
return ((rnd.Next(10) % 2) == 0);
}
Hope it is clear enough.