vb.net 如何关闭子窗体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12987173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:59:34  来源:igfitidea点击:

How to close the child form

vb.netwinforms

提问by Gopal

Using MDI and Child Forms

使用 MDI 和子窗体

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.

上面的代码适用于 MDI 窗体(窗体名称为 mdiform1),但不适用于子窗体 1,当我按下退出键时,它正在关闭 MDI 窗体而不是子窗体。

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

我还检查了子表单名称,名称仅为 form1。

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

有什么问题,我需要更改子表单的任何属性。

Need code help

需要代码帮助

回答by Developer

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

回答by Ahmed Shafik

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.

这似乎是因为表单的 (Key preview) 属性。所以尽量在MDIMain表单中设置Key preview False,在子表单中设置为true。

回答by andy

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

每当您尝试触发子表单事件时,它都会触发父表单“form1_KeyDown”事件。