child form closing when it should not

B

Bamse

hi,
I have 2 forms, one is MDI, one is child of the before mentioned one;
when the user tries to close the child, the form is checked whether the data
within changed;
if data changed, the user is prompted to: Save/Not Save/Cancel

if Save is clicked - the data is saved and the form closes
if Not save is clicked - the data is not saved and the form closes
if Cancel is clicked - the data is not saved and the form should NOT close

I've overrided OnClosing() and made a function which returns false if the
user choosed Cancel, setting e.Cancel = true to cancel closing

protected override OnClosing(System.ComponentModel.CancelEventArgs e)
{
if (!IsOkToClose())
e.Cancel = true;
//base.OnClosing(e);
}

this method is mentioned in many sources (MSDN, google, etc)
still, this code does not work, the form closes anyway

someone can help?

Thank you,
Daniel
 
M

Morten Wennevik

Hi Daniel,

Are you sure IsOkToClose() returns the proper value? You can put a breakpoint on the e.Cancel to see if it is ever called (or put a messagebox inside the if brackets).

Happy coding!
Morten Wennevik [C# MVP]
 
M

Morten Wennevik

Well, I can't verify your problem, using the following code in either overriden or subscribed closing event.

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;
}

Could you provide us with some more code or details?

Happy coding!
Morten Wennevik [C# MVP]
 
B

Bamse

if the data is saved/not saved, IsSafeToClose() returns true;
if the user clicks Cancel, IsSafeToClose() returns false;



protected override void OnClosing(System.ComponentModel.CancelEventArgs
ce)
{
ce.Cancel = (!this.IsSafeToClose());
base.OnClosing(ce);
}

I can't put the code whicch checks the data changes because the function
is called from another point in form, when the user closes the current
set of data/opens another one

the issue is that, ce.Cancel is set to true, but the form still unloads
 
M

Morten Wennevik

Sorry, can't help you. The only way I can think of this happening is if the closing event is called two or more times where the later event passes through the check (like accidentally calling Close() at a point where it is close to safe but user didn't close).

Happy coding!
Morten Wennevik [C# MVP]
 
B

Bamse

another issue is that when clicking 'X' button, the code works; when
clicking the "Exit" button which is set as the CancelButton of the form, the
code does not work
 
M

Morten Wennevik

? CancelButton. If by that you mean a button set to DialogResult.Cancel you need to call close in the button's click event.

Happy coding!
Morten Wennevik [C# MVP]
 
M

Morten Wennevik

Ah, setting a button to Cancel has the effect of setting DialogResult.Cancel, you have to call this.Close() in the button event.

Happy coding!
Morten Wennevik [C# MVP]
 
B

Bamse

yes, a WindowsForm has 2 properties, AcceptButton - button 'clicked' when
the user presses "Enter" and CancelButton for "Esc"

Morten Wennevik said:
? CancelButton. If by that you mean a button set to DialogResult.Cancel
you need to call close in the button's click event.
Happy coding!
Morten Wennevik [C# MVP]
 
M

Morten Wennevik

My bad *blush*. Yeah, the CancelButton. Trying it on a form using ShowDialog and clicking a button set as CancelButton caused a Closing event which handled e.Cancel as expected. I'm not sure why it doesn't work for you :(

Happy coding!
Morten Wennevik [C# MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top