Creating a confirmation box when clicking on the X

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dcurvez
    New Member
    • Apr 2010
    • 5

    Creating a confirmation box when clicking on the X

    I am wondering how I would create and code a box that asks if you want to close when you try to close out by hitting the x in the window bar of my form?

    I dont want to disable the standard issued close..but i want them to confirm close before closing a window. I have my buttons that confirm this...but not for when they hit the close box up top...my form just closes. :(

    I am using Visual studio.net with vb.net codes (so far..)
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    You want to handle the Form.Closing event, then ask your question to the user.

    In C# it looks like this. You should be able to translate it fairly easily.
    Code:
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                this.TopMost = true; // so that our dialog will be on top and seen.
                System.Windows.Forms.DialogResult result = MessageBox.Show(this,"Are you sure", "Close?", 
                                                                           MessageBoxButtons.YesNo,
                                                                           MessageBoxIcon.Question);
                if (result == System.Windows.Forms.DialogResult.No) e.Cancel = true; // Cancel the close
            }

    Comment

    • Dcurvez
      New Member
      • Apr 2010
      • 5

      #3
      confirmation closing box

      i am wondering if anyone can tell me how to get a confirmation box before closing when people close a window by using the x to close? i am using vb.net

      Comment

      • MrMancunian
        Recognized Expert Contributor
        • Jul 2008
        • 569

        #4
        You declare a variable as MsgBoxResult, assigning a MsgBox to it with a MsgBoxStyle.Yes NoCancel. After that, you check the value of the variable you created. Possible values are MsgBoxResult.Ye s or MsgBoxResult.No . Then, according to the outcome, do something :-)

        Steven

        Comment

        • balabaster
          Recognized Expert Contributor
          • Mar 2007
          • 798

          #5
          Originally posted by MrMancunian
          You declare a variable as MsgBoxResult, assigning a MsgBox to it with a MsgBoxStyle.Yes NoCancel. After that, you check the value of the variable you created. Possible values are MsgBoxResult.Ye s or MsgBoxResult.No . Then, according to the outcome, do something :-)

          Steven
          You missed a vital point that this needs to be done in the form's closing event.

          @Dcurvez Create an event handler for the Form.Closing event, in the event handler, fire a confirmation box in the manner suggested by MrMancunian. If the result is .No or .Cancel then call e.Cancel (e is of type FormClosingEven tArgs - calling cancel prevents the window closing). This event handler will fire even if the form is closed using the control toolbox or the x icon in the top right corner.

          Comment

          • MrMancunian
            Recognized Expert Contributor
            • Jul 2008
            • 569

            #6
            True, I missed that ;-)

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Please don't create second threads for the same question.
              This was already answered, complete with code in:

              The two threads have been merged

              Comment

              • balabaster
                Recognized Expert Contributor
                • Mar 2007
                • 798

                #8
                @tlhintoq That could also be tidied up slightly by condensing:

                Code:
                this.topmost = true;
                e.cancel = MessageBox.Show(this,
                    "Are you sure", 
                    "Close?",  
                    MessageBoxButtons.YesNo, 
                    MessageBoxIcon.Question) == DialogResult.No;
                It saves a check operation in the cases where No will be pushed and it's just as performant to set the value true as it is to check the result of the user input.

                Comment

                • Dcurvez
                  New Member
                  • Apr 2010
                  • 5

                  #9
                  I am sorry for the double post but for some reason when I got the first reply from tlhintoq I was not able to see the response. I checked and waited for a bit thinking it was just some fluke thing..but still nothing, so I decided to go and retry posting again. I did not mean to muck up anything..and I want to thank all of you that took the time to reply, it was all most helpful :)

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    I thought I would toss in my typical set of exit methods (including the nice addition from BalaBaster)

                    Code:
                    // Menu:  File | Exit
                    private void closeToolStripMenuItem_Click(object sender, EventArgs e)
                    {
                        LogThis("File | Exit", tlhintoq.Logger.LogEvents.DEFAULT);
                        this.Close();
                    }
                    
                    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
                    {
                        LogThis("Closing");
                        ConfirmQuit(e);
                    }
                    
                    void ConfirmQuit(FormClosingEventArgs e)
                    {
                        LogThis("Confirm Quit");
                        this.TopMost = true;
                        e.Cancel = MessageBox.Show(this,
                                                    "Are you sure",
                                                    "Close?",
                                                    MessageBoxButtons.YesNo,
                                                    MessageBoxIcon.Question) == DialogResult.No;
                        
                        // True means cancel the close
                        // So false means don't cancel, we *are* closing.
                        if (!e.Cancel)
                        {
                            Stop();// Politely stop all workers and other "Stop" behaviors
                            Quit();// Perform clean up, save status, check for threads not yet aborted, then quite the application
                        }
                    }
                    
                    private void Quit()
                    {
                        LogThis("Quitting");
                        tlhintoq.Environment.ThisPC.FindAndKillProcess(Application.ProductName);//Search out threads and kill them
                        Application.Exit();
                        Environment.Exit(0);
                    }

                    Comment

                    Working...