Gopal
Gopal

Reputation: 11982

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

Upvotes: 0

Views: 8946

Answers (3)

Ahmed Shafik
Ahmed Shafik

Reputation: 1

It seems to be because of the (Key preview) property of the forms. So try to set Key preview False in MDIMain form, And set it true in the child form.

Upvotes: 0

Developer
Developer

Reputation: 8636

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

Upvotes: 3

andy
andy

Reputation: 6079

Whenever your trying to fire the Child form event its firing the parent form "form1_KeyDown" event.

Upvotes: 0

Related Questions