child form closing when it should not

Discussion in 'Microsoft Dot NET Framework Forms' started by Bamse, May 25, 2004.

  1. Bamse

    Bamse Guest

    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
     
    Bamse, May 25, 2004
    #1
    1. Advertisements

  2. 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]
     
    Morten Wennevik, May 25, 2004
    #2
    1. Advertisements

  3. Bamse

    Bamse Guest

    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!
     
    Bamse, May 25, 2004
    #3
  4. 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]
     
    Morten Wennevik, May 25, 2004
    #4
  5. Bamse

    Bamse Guest

    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!
     
    Bamse, May 25, 2004
    #5
  6. 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]
     
    Morten Wennevik, May 25, 2004
    #6
  7. Bamse

    Bamse Guest

    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:...
    >
    >
    > 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!
     
    Bamse, May 25, 2004
    #7
  8. ? 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]
     
    Morten Wennevik, May 25, 2004
    #8
  9. 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]
     
    Morten Wennevik, May 25, 2004
    #9
  10. Bamse

    Bamse Guest

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

    "Morten Wennevik" <> wrote in message
    news:blush:pr8j120wuklbvpo@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]
     
    Bamse, May 25, 2004
    #10
  11. 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]
     
    Morten Wennevik, May 25, 2004
    #11
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    Opening a child form from another child form

    John, Dec 31, 2003, in forum: Microsoft Dot NET Framework Forms
    Replies:
    3
    Views:
    273
    DRaiko
    Jan 2, 2004
  2. Marina Neyman

    MDI - how to open a child from a child within MDI parent form?

    Marina Neyman, Feb 7, 2004, in forum: Microsoft Dot NET Framework Forms
    Replies:
    0
    Views:
    846
    Marina Neyman
    Feb 7, 2004
  3. Eddie B.

    How to launch a child form a child form?

    Eddie B., Mar 4, 2004, in forum: Microsoft Dot NET Framework Forms
    Replies:
    1
    Views:
    222
    Herfried K. Wagner [MVP]
    Mar 4, 2004
  4. Michael Wong

    child form Closing() event not fired

    Michael Wong, Jan 8, 2005, in forum: Microsoft Dot NET Framework Forms
    Replies:
    0
    Views:
    213
    Michael Wong
    Jan 8, 2005
  5. kstuver
    Replies:
    1
    Views:
    662
    Michael Höhne
    Dec 12, 2005
Loading...

Share This Page