Esc Key Event on VB 6 Form
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Esc Key Event on VB 6 Form

  1. #1
    Join Date
    Apr 2011
    Posts
    1

    Esc Key Event on VB 6 Form

    I have a form with a frame. i want to close form with Esc Key and i m doing this. But when frame is visible then it should hide frame but its closing form. Is there any way to hide frame when frame is visible with Esc Key and close form when frame is hidden.

    i am using this.

    Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyEscape Then
    If frm1.Visible = True Then
    frm1.Visible = False
    Else
    Unload Me
    End If
    End If
    End Sub

    Any other ways to do this.?

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    Re: Esc Key Event on VB 6 Form

    I tried this with a blank form and a single frame called frm1.

    Your code worked perfectly.
    When I ran the code, the first time I pressed the esc key the frame was hidden, then the next time the esc key was pressed the form closed.

    Is there something I am missing?

  3. #3
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,783

    Re: Esc Key Event on VB 6 Form

    Make sure keypreview is turned on otherwise (depending on what has focus) the event may not fire when the ESC key is pressed. Keypreview will allow the event to fire regaurdless if a text box may have the focus or some other control on the form.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,074

    Re: Esc Key Event on VB 6 Form

    Tried it with a textbox having the focus as well, and I also have not had any issues?

    Is there perhaps a timer or something doing some stuff as well?

  5. #5
    Join Date
    Jul 2012
    Posts
    2

    Re: Esc Key Event on VB 6 Form

    Try this code. Let say you have form1 and form2. You are currently oppened form2 and want to move back to form1 by pressing Esc key. For this use this key

    Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyEscape Then
    form1.show
    frm2.hide
    End Sub

    Hope i ve clear ur doub.

    Thanks

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,074

    Re: Esc Key Event on VB 6 Form

    Quote Originally Posted by yongstars View Post
    Try this code. Let say you have form1 and form2. You are currently oppened form2 and want to move back to form1 by pressing Esc key. For this use this key

    Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyEscape Then
    form1.show
    frm2.hide
    End Sub

    Hope i ve clear ur doub.

    Thanks
    I think this problem has been solved, because this thread is over a YEAR OLD. Please do not revive old threads. It messes up forum flow, and there are numerous current threads that need input.

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center