activemdichild.closed...

  • Thread starter Thread starter Bad_Kid
  • Start date Start date
B

Bad_Kid

I would like to make some event in the MAIN form:
when activeMDIchild is closed (clicked X on the top right side of the
screen) to display a message like "are you sure", or "would you like to save
your work...", -> if user wants to save it - to save and then close; or else
just close that MDIchild
???
(I did something but everytime compiler says An unhandled exception of type
'System.NullReferenceException' occurred in Projekt iz temp.exe)
???
 
Hi Bad_Kid,

The NullReferenceException means you are trying to do something with an
object that either haven't been created yet, or doesn't exist anymore
(reference to the object is null).

The behaviour of asking to save the work should probably be given to the
child instead of the parent. Then just capture the Closing event in the
child and ask the user to save (if the document is "dirty"). Closing the
parent will cause each child to ask if you want to save, and if you cancel
one of them, the parent won't close either.

private void Child_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
DialogResult result = MessageBox.Show(
this,
"Do you want to save your work?",
"MyApp",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1);

switch(result)
{
case DialogResult.Yes:
// do save stuff
// clean up?
break;
case DialogResult.No:
// clean up?
break;
case DialogResult.Cancel:
e.Cancel = true;
break;
}
}

Happy coding!
Morten Wennevik [C# MVP]
 
but how to cancel closing that form after X is pressed???


Hi Bad_Kid,

The NullReferenceException means you are trying to do something with an
object that either haven't been created yet, or doesn't exist anymore
(reference to the object is null).

The behaviour of asking to save the work should probably be given to the
child instead of the parent. Then just capture the Closing event in the
child and ask the user to save (if the document is "dirty"). Closing the
parent will cause each child to ask if you want to save, and if you cancel
one of them, the parent won't close either.

private void Child_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
DialogResult result = MessageBox.Show(
this,
"Do you want to save your work?",
"MyApp",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1);

switch(result)
{
case DialogResult.Yes:
// do save stuff
// clean up?
break;
case DialogResult.No:
// clean up?
break;
case DialogResult.Cancel:
e.Cancel = true;
break;
}
}

Happy coding!
Morten Wennevik [C# MVP]
 
Setting e.Cancel = true will cancel the closing.

Happy coding!
Morten Wennevik [C# MVP]
 
Back
Top