locked
DialogResult Problem on FormClosing Event RRS feed

  • Question

  • Hi there I'm having a strange problem that have never had before, Please see if you can help me figure it out.

    No matter what button I press, I still get the "Did not work!" MessageBox.
    Here's my code for the FormClosing Event:


    System::Windows::Forms::DialogResult^ Confirm = MessageBox::Show("Would you like to save before you exit?\n"

    , "Confirm Exit", MessageBoxButtons::YesNoCancel, MessageBoxIcon::Question);

    if (Confirm == System::Windows::Forms::DialogResult::Yes)

    {

    MessageBox::Show("Saving... " + Confirm);

    }

    else if (Confirm == System::Windows::Forms::DialogResult::No)

    {

    MessageBox::Show("Closing... " + Confirm);

    // close the form without saving

    Application::Exit();

    }

    else if (Confirm == System::Windows::Forms::DialogResult::Cancel)

    {

    MessageBox::Show("Returned! " + Confirm);

    // abort form closing and return to the application

    e->Cancel = true;

    }

    else

    MessageBox::Show("Did not work!");


    I have also tried this:

    e->Cancel = false;

    System::Windows::Forms::DialogResult^ Confirm = MessageBox::Show(

    String::Concat("Would you like to save before you exit?\n", "Confirm Exit",MessageBoxButtons::YesNoCancel,

    MessageBoxIcon::Question,MessageBoxDefaultButton::Button1,MessageBoxOptions::DefaultDesktopOnly, false);

    if (Confirm == System::Windows::Forms::DialogResult::Yes)

    {

    MessageBox::Show("Saving...");

    }

    else if (Confirm == System::Windows::Forms::DialogResult::No)

    {

    MessageBox::Show("Closing...");

    // close the form without saving

    Application::Exit();

    }

    else

    {

    MessageBox::Show("Did not work!");

    // abort form closing and return to the application

    e->Cancel = true;

    }


    I don't know what else to try!, Please help me find the problem.
    Thanx.


    Trust No one
    Tuesday, August 19, 2008 10:22 AM

Answers

  • Change the line

    System::Windows::Forms::DialogResult^ Confirm = MessageBox::Show("Would you like to save before you exit?\n", "Confirm Exit", MessageBoxButtons::YesNoCancel, MessageBoxIcon::Question);


    To

    System::Windows::Forms::DialogResult Confirm = MessageBox::Show("Would you like to save before you exit?\n", "Confirm Exit", MessageBoxButtons::YesNoCancel, MessageBoxIcon::Question);

    Note the removal of the ^

    You'll also have to make some changes to the subsequent MessageBox::Show calls as they don't like 'Confirm' being added to a string (try Confirm.ToString() perhaps)

    • Proposed as answer by Neil Tippett Wednesday, August 20, 2008 8:50 AM
    • Marked as answer by switch13 Wednesday, August 20, 2008 9:26 AM
    Tuesday, August 19, 2008 2:38 PM
  • That's because another event is trying to close the form and is therefore correct behaviour.

    Either:

    • filter the calls to FormClosing by the FormClosingEventArgs::CloseReason
    • or call Environment.Exit(-1); //-1 refers to the process exit code
    • Marked as answer by switch13 Wednesday, August 20, 2008 9:00 AM
    • Marked as answer by switch13 Wednesday, August 20, 2008 9:00 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:26 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:26 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Marked as answer by switch13 Wednesday, August 20, 2008 9:32 AM
    Wednesday, August 20, 2008 8:50 AM
  • Damn! that was a fast reply!
    Thanx.

    I also found another solution, Using this:

    e->Cancel = false;

    That seems to work.

    I think the flu that I have is affecting my brain a bit! lol.


    Trust No one
    • Marked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    Wednesday, August 20, 2008 8:56 AM

All replies

  • Change the line

    System::Windows::Forms::DialogResult^ Confirm = MessageBox::Show("Would you like to save before you exit?\n", "Confirm Exit", MessageBoxButtons::YesNoCancel, MessageBoxIcon::Question);


    To

    System::Windows::Forms::DialogResult Confirm = MessageBox::Show("Would you like to save before you exit?\n", "Confirm Exit", MessageBoxButtons::YesNoCancel, MessageBoxIcon::Question);

    Note the removal of the ^

    You'll also have to make some changes to the subsequent MessageBox::Show calls as they don't like 'Confirm' being added to a string (try Confirm.ToString() perhaps)

    • Proposed as answer by Neil Tippett Wednesday, August 20, 2008 8:50 AM
    • Marked as answer by switch13 Wednesday, August 20, 2008 9:26 AM
    Tuesday, August 19, 2008 2:38 PM
  •  Damn! I can't believe its such a stupid error, I did'nt pick that up.

    Thanx for the quick reply.

    I have a problem with this line:

    else if (Confirm == System::Windows::Forms::DialogResult::No)

    {

    MessageBox::Show("Closing... " + Confirm.ToString() );

    // close the form without saving

    Application::Exit();

    It shows the MessageBox again when I click No, then it closes the application.

    Thank you.


    Trust No one
    Wednesday, August 20, 2008 8:39 AM
  • That's because another event is trying to close the form and is therefore correct behaviour.

    Either:

    • filter the calls to FormClosing by the FormClosingEventArgs::CloseReason
    • or call Environment.Exit(-1); //-1 refers to the process exit code
    • Marked as answer by switch13 Wednesday, August 20, 2008 9:00 AM
    • Marked as answer by switch13 Wednesday, August 20, 2008 9:00 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:26 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:26 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Unmarked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    • Marked as answer by switch13 Wednesday, August 20, 2008 9:32 AM
    Wednesday, August 20, 2008 8:50 AM
  • Damn! that was a fast reply!
    Thanx.

    I also found another solution, Using this:

    e->Cancel = false;

    That seems to work.

    I think the flu that I have is affecting my brain a bit! lol.


    Trust No one
    • Marked as answer by switch13 Wednesday, August 20, 2008 9:27 AM
    Wednesday, August 20, 2008 8:56 AM
  • No problem.

    Can you mark which ever posts you feel answered your questions as the post solution please!
    Wednesday, August 20, 2008 8:58 AM
  • WIll do that, even if it's my own post?
    Trust No one
    Wednesday, August 20, 2008 8:59 AM