如何确定关闭的表格?

时间:2014-04-08 16:56:27

标签: .net vb.net winforms events

我确定有一些事件,但我以前从未使用过这个,但我只是想找一些方法来检查隐藏/关闭哪个表单并根据事情做一些功能什么窗口实际上已关闭或隐藏。

因此,当应用程序启动时,会有按钮,每个按钮都会打开一个新窗口。一旦在那些窗口上完成处理,它们将被隐藏并且焦点将返回到原始窗口,有没有办法告诉哪个窗口被隐藏,因为焦点返回到主窗口?

1 个答案:

答案 0 :(得分:1)

您必须将事件处理程序附加到您创建的每个表单:

AddHandler form.FormClosed, AddressOf SomeMethodThatHandlesFormClosed

此方法要求您有两个参数,一个是Object类型,另一个是类型FormClosedEventArgs。通常,它们分别命名为sendere

Sub SomeMethodThatHandlesFormClosed(sender As Object, e As FormClosedEventArgs )
    ' sender parameter represents the closed form instance.
    Dim form As Form = sender

    ' do what you have to do with the closed form here.
End Sub

修改

再次阅读您的问题我注意到您提到隐藏表单,这与关闭不同。在这种情况下,您可能希望处理VisibleChanged事件:

AddHandler form.FormClosed, AddressOf SomeMethodThatHandlesVisibleChanged

' ...

Sub SomeMethodThatHandlesVisibleChanged(sender As Object, e As EventArgs )
    ' sender parameter represents the hidden form instance.
    Dim form As Form = sender

    If Not form.Visible Then
        ' do what you have to do with the hiding form here.
    End If
End Sub