Results 1 to 12 of 12

Thread: How to Close Child Window Using ALT+F4 Key

  1. #1

    Thread Starter
    Member jalzaaal's Avatar
    Join Date
    Feb 2005
    Posts
    63

    How to Close Child Window Using ALT+F4 Key

    Hello All,

    I have a MDI form and a some child forms. when I load any child form it is opened in maximized status in mdi form. But when I press Alt+F4 key the MDI form is closed. What I want to do is ... I want is to close the current child form using Alt+F4 key. If I can close the child form Using CTRL+F4 key then it will also be fine.

    Can Anyone Help Me????

  2. #2

  3. #3

    Thread Starter
    Member jalzaaal's Avatar
    Join Date
    Feb 2005
    Posts
    63

    Re: How to Close Child Window Using ALT+F4 Key

    Where Should I write this Sendkeys...?
    I dont want to do this on button's event.
    what i want is if any child form is currently loaded and user presses Alt+F4 Keys the child form should be unloaded not the Mdi Form.

  4. #4
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: How to Close Child Window Using ALT+F4 Key

    If you search on pscode.com/vb you will find code from key logger apps, which will listen for keys pressed, and you could make it so that if alt+f4 is pressed, unload the active child form, or the child form thats in focus.

    Chris
    Chris

  5. #5
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,728

    Re: How to Close Child Window Using ALT+F4 Key

    The following code will work (almost). Only problem is that, it can't distinguish between Alt+F4 keypress and Close button click.
    VB Code:
    1. Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.  
    3.     'If a child is active, then unload the child. Otherwise unload MDI
    4.     If UnloadMode = vbFormControlMenu Then
    5.         ' User pressed either Alt+F4 or clicked on the Close (X) button
    6.         If Not Me.ActiveForm Is Nothing Then
    7.             Unload Me.ActiveForm
    8.             Cancel = 1
    9.         End If
    10.     End If
    11.    
    12. End Sub
    Last edited by iPrank; Feb 15th, 2006 at 10:16 AM. Reason: Comment edited
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to Close Child Window Using ALT+F4 Key

    Since MDI Forms doen't respond to key events and therefore doesn't have KeyPreview property you need to use some api and timer -= I did it for F4 key (Alt+F4 ot CTRL+F4 is little more complicated).
    So, place a timer on your MDI form, set Interval = 100, then copy/paste the following code directly to your MDI form:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetAsyncKeyState Lib "user32" _
    4.     (ByVal vKey As Long) As Integer
    5.  
    6. Private Const VK_F4 = &H73
    7.  
    8. Private Sub Timer1_Timer()
    9.     If GetAsyncKeyState(VK_F4) Then
    10.         Unload Me.ActiveForm
    11.     End If
    12. End Sub

  7. #7
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: How to Close Child Window Using ALT+F4 Key

    Try this in your MDI Form
    VB Code:
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2.     Dim AltDown
    3.     AltDown = (Shift And vbAltMask) > 0
    4.     If AltDown And KeyCode = vbKeyF4 Then
    5.         Unload MyChildForm 'Use name of the Child Form here
    6.         KeyCode = 0
    7.     End If
    8. End Sub

    EDIT
    Woops, sorry, you're right Rhino, I didn't know that MDI forms don't have this events.
    Last edited by jcis; Feb 15th, 2006 at 10:27 AM.

  8. #8
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,728

    Re: How to Close Child Window Using ALT+F4 Key

    Quote Originally Posted by jcis
    Try this in your MDI Form
    VB Code:
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2.     Dim AltDown
    3.     AltDown = (Shift And vbAltMask) > 0
    4.     If AltDown And KeyCode = vbKeyF4 Then
    5.         Unload MyChildForm 'Use name of the Child Form here
    6.         KeyCode = 0
    7.     End If
    8. End Sub

    EDIT
    Woops, sorry, you're right Rhino, I didn't know that MDI forms don't have this events.
    jcis, your idea is correct.
    Paste it inside every MDIChild, and it will work.
    Slightly modified version,
    VB Code:
    1. 'Inside [b]MDIChild[/b] forms
    2. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    3.     Dim AltDown
    4.     AltDown = (Shift And vbAltMask) > 0
    5.     If AltDown And KeyCode = vbKeyF4 Then
    6.         Unload [b]Me[/b] ' Unload this form
    7.         KeyCode = 0
    8.     End If
    9. End Sub
    If there is no MDIChild, it will unload the MDI form.

    Edit: Oh! The MDI form gets unloaded if no child form has focus. May be we should check if there is any activeform in MDIForm_QueryUnload.
    Last edited by iPrank; Feb 15th, 2006 at 10:41 AM.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  9. #9

  10. #10
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,728

    Re: How to Close Child Window Using ALT+F4 Key

    Well ... the truth is .... I didn't like the timer idea.

    May be subclassing can give some answer ?

    edit: Yes. Yes. I know I use timer a lot. But that's because I can't find any other way.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  11. #11
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to Close Child Window Using ALT+F4 Key

    Quote Originally Posted by iPrank
    Well ... the truth is .... I didn't like the timer idea. ...
    What's wrong with using timer? As of VB4 release there used to be 15 timers allowed per one form (or entire application - can't recall exactly) but that limitation is long gone...

  12. #12
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,728

    Arrow Close MDIChild Window Using ALT+F4 Key (or disable ALT+F4)

    Finally it is done ! (I hope)

    At first I tried removing the Alt+F4 accleator key from system menu. But hours of google searching didn't give anything.

    Now, I'm registering Alt+F4 as hotkey when the MDI activates.

    When the MDI deactivates, I'm unregistering the hotkey.

    So,the hotkey doesn't effect other windows.
    Attached Files Attached Files
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


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