Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Close Button

Close Button

Scheduled Pinned Locked Moved C#
helptutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alee15 10 88
    wrote on last edited by
    #1

    I would like to know how to display a message box with an "Are you sure you want to exit?" when someone clicks the close button, therefore the cross button. I managed to do it in a simple button but I think it's different in the close button! Can someone help me please?! Thanks ;) Alison

    J L 2 Replies Last reply
    0
    • A alee15 10 88

      I would like to know how to display a message box with an "Are you sure you want to exit?" when someone clicks the close button, therefore the cross button. I managed to do it in a simple button but I think it's different in the close button! Can someone help me please?! Thanks ;) Alison

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      subscribe to the Closing event on a form, and set the Cancel property = true on the event argument if you dont want to exit.

      1 Reply Last reply
      0
      • A alee15 10 88

        I would like to know how to display a message box with an "Are you sure you want to exit?" when someone clicks the close button, therefore the cross button. I managed to do it in a simple button but I think it's different in the close button! Can someone help me please?! Thanks ;) Alison

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Override the OnClosing event of your form:

        this.FormClosing += new FormClosingEventHandler(OnClosing);

        and use this code:

        private void OnClosing(object sender, FormClosingEventArgs e)
        {
        DialogResult res = MessageBox.Show("Are you sure you want to close the form?",
        "Question",
        MessageBoxButtons.YesNoCancel,
        MessageBoxIcon.Question);
        if (res != DialogResult.Yes)
        {
        e.Cancel = true; // do not close this form
        }
        }

        regards -- modified at 10:41 Monday 20th February, 2006

        J A 2 Replies Last reply
        0
        • L Lost User

          Override the OnClosing event of your form:

          this.FormClosing += new FormClosingEventHandler(OnClosing);

          and use this code:

          private void OnClosing(object sender, FormClosingEventArgs e)
          {
          DialogResult res = MessageBox.Show("Are you sure you want to close the form?",
          "Question",
          MessageBoxButtons.YesNoCancel,
          MessageBoxIcon.Question);
          if (res != DialogResult.Yes)
          {
          e.Cancel = true; // do not close this form
          }
          }

          regards -- modified at 10:41 Monday 20th February, 2006

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          Greg, that isn't overriding, that is event subscription. To override, you don't need an event handler:

          protected override void OnClosing(CancelEventArgs e)
          {
          DialogResult res = MessageBox.Show("Are you sure you want to close the form?",
          "Question",
          MessageBoxButtons.YesNoCancel,
          MessageBoxIcon.Question);
          if (res != DialogResult.Yes)
          {
          e.Cancel = true; // do not close this form
          }
          }

          L 1 Reply Last reply
          0
          • L Lost User

            Override the OnClosing event of your form:

            this.FormClosing += new FormClosingEventHandler(OnClosing);

            and use this code:

            private void OnClosing(object sender, FormClosingEventArgs e)
            {
            DialogResult res = MessageBox.Show("Are you sure you want to close the form?",
            "Question",
            MessageBoxButtons.YesNoCancel,
            MessageBoxIcon.Question);
            if (res != DialogResult.Yes)
            {
            e.Cancel = true; // do not close this form
            }
            }

            regards -- modified at 10:41 Monday 20th February, 2006

            A Offline
            A Offline
            alee15 10 88
            wrote on last edited by
            #5

            [Message Deleted]

            L 1 Reply Last reply
            0
            • J Judah Gabriel Himango

              Greg, that isn't overriding, that is event subscription. To override, you don't need an event handler:

              protected override void OnClosing(CancelEventArgs e)
              {
              DialogResult res = MessageBox.Show("Are you sure you want to close the form?",
              "Question",
              MessageBoxButtons.YesNoCancel,
              MessageBoxIcon.Question);
              if (res != DialogResult.Yes)
              {
              e.Cancel = true; // do not close this form
              }
              }

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Yeah, my bad, I mixed up both things. But the code works nonetheless :-O

              1 Reply Last reply
              0
              • A alee15 10 88

                [Message Deleted]

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                this.FormClosing += new FormClosingEventHandler(OnClosing); This should be put into the form constructor

                A 1 Reply Last reply
                0
                • L Lost User

                  this.FormClosing += new FormClosingEventHandler(OnClosing); This should be put into the form constructor

                  A Offline
                  A Offline
                  alee15 10 88
                  wrote on last edited by
                  #8

                  Ohh I made it, Thanks a lot people for helping me! :-D

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups