Gopal Gopal - 1 month ago 9
Vb.net Question

How to close the child form

Using MDI and Child Forms

Code.

childform_load

Me.MdiParent = MDIMain


'

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


The above code is working for MDI Form (form name is mdiform1), but not working for child form1, when I press the escape key, it is closing the MDI Form instead of Child form.

I check the Child Form Name also, name is form1 only.

What was the problem, i need to change any property of child form.

Need code help

Answer

This works for me

Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Escape Then
            For Each child As Form In Me.MdiParent.MdiChildren
                child.Close()
            Next child
        End If
    End Sub