PC Review


Reply
Thread Tools Rate Thread

child form closing when it should not

 
 
Bamse
Guest
Posts: n/a
 
      25th May 2004
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


 
Reply With Quote
 
 
 
 
Morten Wennevik
Guest
Posts: n/a
 
      25th May 2004
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]
 
Reply With Quote
 
 
 
 
Bamse
Guest
Posts: n/a
 
      25th May 2004


Of course it returns the right value, because e.Cancel = true gets
executed;

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Morten Wennevik
Guest
Posts: n/a
 
      25th May 2004
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]
 
Reply With Quote
 
Bamse
Guest
Posts: n/a
 
      25th May 2004


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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Morten Wennevik
Guest
Posts: n/a
 
      25th May 2004
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]
 
Reply With Quote
 
Bamse
Guest
Posts: n/a
 
      25th May 2004
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

"Bamse" <mymail> wrote in message
news:(E-Mail Removed)...
>
>
> 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
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
Morten Wennevik
Guest
Posts: n/a
 
      25th May 2004
? 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]
 
Reply With Quote
 
Morten Wennevik
Guest
Posts: n/a
 
      25th May 2004
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]
 
Reply With Quote
 
Bamse
Guest
Posts: n/a
 
      25th May 2004
yes, a WindowsForm has 2 properties, AcceptButton - button 'clicked' when
the user presses "Enter" and CancelButton for "Esc"

"Morten Wennevik" <(E-Mail Removed)> wrote in message
newspr8j120wuklbvpo@morten_x.edunord...
> ? 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]



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
child of a child of a child on a form. wdsnews Microsoft Access 9 17th Mar 2008 09:27 PM
Closing event in a MID Child form I don't know if the child form is closing or the main form is closing **Developer** Microsoft C# .NET 1 19th Oct 2005 04:51 PM
What should you do when closing a child form/dialog? =?Utf-8?B?TXJOb2JvZHk=?= Microsoft C# .NET 0 5th Jan 2005 03:55 AM
Opening a child form from another child form John Microsoft Dot NET Framework Forms 3 2nd Jan 2004 11:27 AM
setting 2nd form as a child form, from first child form Rat Microsoft VB .NET 2 1st Dec 2003 12:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:58 AM.