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????
Re: How to Close Child Window Using ALT+F4 Key
You can use SendKeys("%{F4}") from a button click but what's the point?
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.
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
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:
Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'If a child is active, then unload the child. Otherwise unload MDI
If UnloadMode = vbFormControlMenu Then
' User pressed either Alt+F4 or clicked on the Close (X) button
If Not Me.ActiveForm Is Nothing Then
Unload Me.ActiveForm
Cancel = 1
End If
End If
End Sub
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:
Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
Private Const VK_F4 = &H73
Private Sub Timer1_Timer()
If GetAsyncKeyState(VK_F4) Then
Unload Me.ActiveForm
End If
End Sub
Re: How to Close Child Window Using ALT+F4 Key
Try this in your MDI Form
VB Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim AltDown
AltDown = (Shift And vbAltMask) > 0
If AltDown And KeyCode = vbKeyF4 Then
Unload MyChildForm 'Use name of the Child Form here
KeyCode = 0
End If
End Sub
EDIT
Woops, sorry, you're right Rhino, I didn't know that MDI forms don't have this events.
Re: How to Close Child Window Using ALT+F4 Key
Quote:
Originally Posted by jcis
Try this in your MDI Form
VB Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim AltDown
AltDown = (Shift And vbAltMask) > 0
If AltDown And KeyCode = vbKeyF4 Then
Unload MyChildForm 'Use name of the Child Form here
KeyCode = 0
End If
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. :thumb:
Paste it inside every MDIChild, and it will work.
Slightly modified version,
VB Code:
'Inside [b]MDIChild[/b] forms
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim AltDown
AltDown = (Shift And vbAltMask) > 0
If AltDown And KeyCode = vbKeyF4 Then
Unload [b]Me[/b] ' Unload this form
KeyCode = 0
End If
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.
Re: How to Close Child Window Using ALT+F4 Key
Quote:
Originally Posted by iPrank
...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...
Just use what I posted... ;)
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. :p
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? :confused: 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...
1 Attachment(s)
Close MDIChild Window Using ALT+F4 Key (or disable ALT+F4)
Finally it is done ! (I hope) http://www.vbforums.com/images/ieimages/2006/02/1.gif
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.