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. How to Stop the Form from Closing...

How to Stop the Form from Closing...

Scheduled Pinned Locked Moved C#
helptutorialquestion
6 Posts 3 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.
  • M Offline
    M Offline
    max29297
    wrote on last edited by
    #1

    Is there a way to stop the form from closing within its OnClosing event? I'm making a program that asks if you really want to close it when you press the "x". So I've got a dialog that pops up and conditional statements for whichever button is pressed in the dialog, but I'm not sure what to put in the conditional statements to make the form stop closing. Could anyone help me out with this?


    If I had a sig, it would probably go here.


    M L 2 Replies Last reply
    0
    • M max29297

      Is there a way to stop the form from closing within its OnClosing event? I'm making a program that asks if you really want to close it when you press the "x". So I've got a dialog that pops up and conditional statements for whichever button is pressed in the dialog, but I'm not sure what to put in the conditional statements to make the form stop closing. Could anyone help me out with this?


      If I had a sig, it would probably go here.


      M Offline
      M Offline
      martin_hughes
      wrote on last edited by
      #2

      Yep:

      private void Form1_FormClosing(object sender, FormClosingEventArgs e)
      {

              DialogResult result = MessageBox.Show("Are you sure you wish to cancel?", "Cancel?", MessageBoxButtons.YesNo);
              if (result == DialogResult.No)
              {
                  e.Cancel = true;
      
              }
      

      }

      "It was the day before today.... I remember it like it was yesterday." -Moleman

      1 Reply Last reply
      0
      • M max29297

        Is there a way to stop the form from closing within its OnClosing event? I'm making a program that asks if you really want to close it when you press the "x". So I've got a dialog that pops up and conditional statements for whichever button is pressed in the dialog, but I'm not sure what to put in the conditional statements to make the form stop closing. Could anyone help me out with this?


        If I had a sig, it would probably go here.


        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        yes, see martin's reply. Thats exactly why the Closing event exists, so you can prevent a Closed event from happening. :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        M 1 Reply Last reply
        0
        • L Luc Pattyn

          yes, see martin's reply. Thats exactly why the Closing event exists, so you can prevent a Closed event from happening. :)

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          M Offline
          M Offline
          max29297
          wrote on last edited by
          #4

          Thanks guys!


          If I had a sig, it would probably go here.


          L 1 Reply Last reply
          0
          • M max29297

            Thanks guys!


            If I had a sig, it would probably go here.


            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            You're welcome.

            max29297 wrote:

            If I had a sig, it would probably go here

            You probably mean: I'm happy I dont need a sig anymore...

            Luc Pattyn


            try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


            M 1 Reply Last reply
            0
            • L Luc Pattyn

              You're welcome.

              max29297 wrote:

              If I had a sig, it would probably go here

              You probably mean: I'm happy I dont need a sig anymore...

              Luc Pattyn


              try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


              M Offline
              M Offline
              max29297
              wrote on last edited by
              #6

              lol, yeah, right


              If I had a sig, it would probably go here.


              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