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. Windows Forms
  4. How to close a window when Esc is pressed

How to close a window when Esc is pressed

Scheduled Pinned Locked Moved Windows Forms
tutorial
3 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.
  • R Offline
    R Offline
    Ragu Rangasamy
    wrote on last edited by
    #1

    I want to know how to close the windows form when Esc button is pressed.

    Rangasamy

    L M 2 Replies Last reply
    0
    • R Ragu Rangasamy

      I want to know how to close the windows form when Esc button is pressed.

      Rangasamy

      L Offline
      L Offline
      LeandroABorges
      wrote on last edited by
      #2

      Hi Ragu, Firsy you have to change this property on form KeyPreview = true You can change this on the form properties in designer or in the form load method:

      private void Form1\_Load(object sender, EventArgs e)
      {
        this.KeyPreview = true;
      }
      

      And then, you only have to do this:

      private void Form1\_KeyDown(object sender, KeyEventArgs e)
      {
        if (e.KeyCode == Keys.Escape)
          this.Close();
      }
      

      That´s it?

      ----- LeandroAB

      1 Reply Last reply
      0
      • R Ragu Rangasamy

        I want to know how to close the windows form when Esc button is pressed.

        Rangasamy

        M Offline
        M Offline
        Mike Dimmick
        wrote on last edited by
        #3

        Set the form's CancelButton property to a button that will close the form.

        "Multithreading is just one damn thing after, before, or simultaneous with another." - Andrei Alexandrescu

        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