Results 1 to 4 of 4

Thread: Closing form using escape key

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2004
    Posts
    70

    Resolved Closing form using escape key

    Hi!

    I need the key escape to close the form that is being used at the moment. I've used keypressed property = key.escape. The problem is that I got a main form which is a parent of the rest and when I press escape it understands that I want to close the parent form instead of the child form (which is opened) and it closes the parent form (and therefore the child form too).

    How can I make it close only the form that is opened and not the parent one?

    Thanks for your help, folks!
    Last edited by nacho2; Jan 4th, 2005 at 10:42 AM.

  2. #2
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382

    Re: Closing form using escape key

    This worked fine for me.. It unloaded Form2 while Form1 stayed open


    Code:
        Private Sub Form2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    
            If e.KeyCode = Keys.Escape Then Me.Close()
    
        End Sub

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Closing form using escape key

    do you have a close button on the form?? if so, in the FORMS properties, set the CancelButton property to that commandbutton, and when you hit escape, it will be the same as clicking that button... likewise the form has an acceptbutton property for when you hit enter

    these properties used to be on the button level, but in .net they have been moved (rightfully so) to the form level

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2004
    Posts
    70

    Re: Closing form using escape key

    Alright, great linking the close button woks great, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width