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. C#: how to close form when esc key is pressed

C#: how to close form when esc key is pressed

Scheduled Pinned Locked Moved C#
csharptutorial
7 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.
  • K Offline
    K Offline
    kssknov
    wrote on last edited by
    #1

    hi my code is not working .can any one suggest me. private void Form1_KeyPress(object sender, KeyPressEventArgs e) { if(e.KeyChar==(char)27) { this.Hide(); } }

    ssk

    G M 2 Replies Last reply
    0
    • K kssknov

      hi my code is not working .can any one suggest me. private void Form1_KeyPress(object sender, KeyPressEventArgs e) { if(e.KeyChar==(char)27) { this.Hide(); } }

      ssk

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      You are hiding your form, not closing. If you want to close your form use this.Close() Also, if you have controls on the form, make sure that your form is processing key press until they reach the controls

      #region signature my articles #endregion

      1 Reply Last reply
      0
      • K kssknov

        hi my code is not working .can any one suggest me. private void Form1_KeyPress(object sender, KeyPressEventArgs e) { if(e.KeyChar==(char)27) { this.Hide(); } }

        ssk

        M Offline
        M Offline
        mav northwind
        wrote on last edited by
        #3

        Hi! The simplest way is to assign the button your users are supposed to close the form (Cancel, Close, ...) to the CancelButton property of your form.

        Regards, mav -- Black holes are the places where God divided by 0...

        K 2 Replies Last reply
        0
        • M mav northwind

          Hi! The simplest way is to assign the button your users are supposed to close the form (Cancel, Close, ...) to the CancelButton property of your form.

          Regards, mav -- Black holes are the places where God divided by 0...

          K Offline
          K Offline
          kssknov
          wrote on last edited by
          #4

          can u tell in detail. i am a beginner. i never used the Cancelbutton of a form thanks

          senthil

          G 1 Reply Last reply
          0
          • K kssknov

            can u tell in detail. i am a beginner. i never used the Cancelbutton of a form thanks

            senthil

            G Offline
            G Offline
            Giorgi Dalakishvili
            wrote on last edited by
            #5

            You can add a button on the form and close the form when that button is clicked. Then in the properties of the form find Cancelbutton property and set it to the button which closes your form.

            #region signature my articles #endregion

            1 Reply Last reply
            0
            • M mav northwind

              Hi! The simplest way is to assign the button your users are supposed to close the form (Cancel, Close, ...) to the CancelButton property of your form.

              Regards, mav -- Black holes are the places where God divided by 0...

              K Offline
              K Offline
              kssknov
              wrote on last edited by
              #6

              ya its working. but one doubt, if i need this same task to be done without creating a button and setting its property to cancelbutton means , how ? dont think i am irritating u

              senthil

              M 1 Reply Last reply
              0
              • K kssknov

                ya its working. but one doubt, if i need this same task to be done without creating a button and setting its property to cancelbutton means , how ? dont think i am irritating u

                senthil

                M Offline
                M Offline
                mav northwind
                wrote on last edited by
                #7

                Allowing the user to close a dialog by using the ESC key only is bad UI design style, so if you keep up a good style and provide a button you don't face the problem.

                Regards, mav -- Black holes are the places where God divided by 0...

                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