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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. how to prevent window close by clicking window close button

how to prevent window close by clicking window close button

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

    hi, any method prevent the window close when user click the "little cross" button at the right top corner of the window. in other word, would i can to get the window close message invoke by the "little cross" button? so, i can re-route the message to prevent window close. anyone would help? thanks, jim

    S F N J 4 Replies Last reply
    0
    • Z zecodela

      hi, any method prevent the window close when user click the "little cross" button at the right top corner of the window. in other word, would i can to get the window close message invoke by the "little cross" button? so, i can re-route the message to prevent window close. anyone would help? thanks, jim

      S Offline
      S Offline
      Stephane David
      wrote on last edited by
      #2

      I'm not sure of the exact syntaxe because I don't have Visual Studio .Net installed. But you can simply select the event closing (I hope it's this one). Then, in the event arg, you can simply use e.Cancel = true; to cancel the event, i.e. prevent the window close. Of course, you could also remove the "little cross" button.

      1 Reply Last reply
      0
      • Z zecodela

        hi, any method prevent the window close when user click the "little cross" button at the right top corner of the window. in other word, would i can to get the window close message invoke by the "little cross" button? so, i can re-route the message to prevent window close. anyone would help? thanks, jim

        F Offline
        F Offline
        Furty
        wrote on last edited by
        #3

        First subscribe to the Closing event:

        this.Closing += new System.ComponentModel.CancelEventHandler(Form_Closing);

        Then use the CancelEventArgs.Cancel property to abort closing:

        private void Form_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
        e.Cancel = true;
        }

        1 Reply Last reply
        0
        • Z zecodela

          hi, any method prevent the window close when user click the "little cross" button at the right top corner of the window. in other word, would i can to get the window close message invoke by the "little cross" button? so, i can re-route the message to prevent window close. anyone would help? thanks, jim

          N Offline
          N Offline
          Nick Seng
          wrote on last edited by
          #4

          Use the OnClosing event to do it.

          private void Form_OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
          {
          e.Cancel = true; //prevents the form from closing
          }


          Support Bone

          S 1 Reply Last reply
          0
          • N Nick Seng

            Use the OnClosing event to do it.

            private void Form_OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
            {
            e.Cancel = true; //prevents the form from closing
            }


            Support Bone

            S Offline
            S Offline
            sumeat
            wrote on last edited by
            #5

            private void Form_OnClosing(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; //prevents the form from closing } This will prevent the form from closing ever!!!!! You do want the user to close the form somehow if not by clicking on the "cross"??

            N 1 Reply Last reply
            0
            • Z zecodela

              hi, any method prevent the window close when user click the "little cross" button at the right top corner of the window. in other word, would i can to get the window close message invoke by the "little cross" button? so, i can re-route the message to prevent window close. anyone would help? thanks, jim

              J Offline
              J Offline
              Jim Stewart
              wrote on last edited by
              #6

              I would like to remind you that your users will expect to be able to close their windows this way. It seems to me to be a big violation of good GUI principles to proceed this way. I would suggest that it would be far better to disable (probably difficult) or not show the close box. In this way, your users won't be confused with the lack of function.

              α.γεεκ

              Fortune passes everywhere.
              Duke Leto Atreides

              1 Reply Last reply
              0
              • S sumeat

                private void Form_OnClosing(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; //prevents the form from closing } This will prevent the form from closing ever!!!!! You do want the user to close the form somehow if not by clicking on the "cross"??

                N Offline
                N Offline
                Nick Seng
                wrote on last edited by
                #7

                This is just a sample. I'd assume zecodela would know enough to put some conditional statements in there before setting e.cancel to true.


                Support Bone

                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