none
Form losing focus after Messagebox.show("..") RRS feed

  • Question

  • In my application, when the user clicks close, i pop up a messagebox, asking them if they are sure they want to close the application and whether they want to save their work. But after they have clicked either yes / no / cancel, the application loses focus and whatever program is underneath my application is placed on top.

    I have tried
                 this.Focus();
                this.BringToFront();
                this.Select();
                TopLevel = true;
                this.Activate();

    But it does nothing. There doesnt seem to be any other obvious calls i can make which should put the application back on top that i can see. Am sure this is just an obvious call, but....

    Thanks


    Wednesday, April 25, 2007 8:45 AM

Answers

  • Odd problem.  Try the MessageBox.Show() overload that takes the owner argument and pass "this".  For example:

        private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
          if (DialogResult.No == MessageBox.Show(this, "Exit without saving changes?", "Exit",
            MessageBoxButtons.YesNo, MessageBoxIcon.Question)) e.Cancel = true;
        }

    Wednesday, April 25, 2007 9:38 AM
    Moderator

All replies

  • Are you subscribed to the FormClosing or FormClosed event? If you subscribe to the FormClosing event you can cancel the action, eg:

    Code Snippet

            private void Form1_FormClosing( object sender, FormClosingEventArgs e )
            {
                if( MessageBox.Show( "Are you sure you want to exit", "Application Exiting", MessageBoxButtons.YesNo ) == DialogResult.No )
                {
                    e.Cancel = true;
                }
            }


    Wednesday, April 25, 2007 9:20 AM
  •  

    Hi

    You should use the FormClosing event so that you can prevent the form been closed. Sample code:

     

         

    Code Snippet
           void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                DialogResult re = MessageBox.Show("dfdf", "dfdfdfdf", MessageBoxButtons.YesNoCancel);
                if (re == DialogResult.OK)
                {
                    //SaveMethod
                }
                else if (re == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
            }

     

    Hope it will help you.

    Wednesday, April 25, 2007 9:31 AM
  • Thanks for your swift replies. I should have made myself clearer.

    It is not an actual form close. The user clicks a button, which closes the file(image) the user is currently working on and puts them back to the welcome screen.

    The actual form that the work screen is on and the welcome screen is on are the same, so there is no form closing event.

    It is weird because, just now i thought i had it solved with an additional

    this.windowstate = FormWindowState.Normal;

    at the end. It worked a couple of times, but now has stopped working. Makes me think there is some additional code being called hidden in the depths of my program.....



    Wednesday, April 25, 2007 9:37 AM
  • Odd problem.  Try the MessageBox.Show() overload that takes the owner argument and pass "this".  For example:

        private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
          if (DialogResult.No == MessageBox.Show(this, "Exit without saving changes?", "Exit",
            MessageBoxButtons.YesNo, MessageBoxIcon.Question)) e.Cancel = true;
        }

    Wednesday, April 25, 2007 9:38 AM
    Moderator
  •  

    I've got exactly the same problem!

    This is the code:

    Code Block

    System::Windows::Forms::DialogResult result;


    result = Windows::Forms::MessageBox::Show(
         String::Concat("Do you want to save before closing?"),

    "Warning!",MessageBoxButtons::YesNoCancel,MessageBoxIcon::Question,MessageBoxDefaultButton::Button1,MessageBoxOptions::DefaultDesktopOnly, false);

     

    if ( result == ::DialogResult::Yes )
    {
         //save file...

    }
    else if (result == ::DialogResult::Cancel)
    {
         //abort.

    return;

    }

     

    //continue with closing the file...

     

     

    As soon as the MessageBox is gone the whole application moves in the background (is not minimized!) and other open applications (like the VC++ Express Edition Editor) which were hidden by my application before become focused.

     

    I tried the same the original poster tried as well but without success.

    Is there any solution?

     

     

    ----

    Rekado

    Thursday, November 22, 2007 8:39 AM
  • Unless you really need it, try removing MessageBoxOptions.DefaultDesktopOnly from your MessageBox.

     

     

    Tuesday, December 4, 2007 1:20 PM
  •  

    Thanks a lot, that really helped! I wasn't even aware of that added option; seems that I just copied that definition from the help... works now, the application stays active and will not be covered by another open application in the background. Great!

    Monday, December 10, 2007 4:04 AM
  • My thanks, that worked, and I didn't need it.
    Thursday, October 18, 2012 3:41 PM
  • Hi

    This is oldie but here I am.

    I've had the same annoyng problem and, at least in my case, ocurring from running the application in debbuging mode (when programming). But when I run it directly from execute file (.exe) it does well and form always keep focused after MessageBox is gone.

    Good luck !!! 

    Saturday, August 8, 2015 2:24 AM