关闭其他窗体时如何刷新一个窗体VB.NET

时间:2019-07-19 17:55:37

标签: vb.net ms-access-2010 vb.net-2010

我想在关闭Form2时刷新Form1, 我进行了很多搜索,但这些查询都没有回答我的问题。

我想在Form1中检测到Form2关闭事件。

2 个答案:

答案 0 :(得分:1)

为什么在Form2的Closed事件中不刷新Form1?

    Private Sub Form2_Closed(sender As Object, e As EventArgs) Handles Me.Closed
        Form1.Refresh()
    End Sub

答案 1 :(得分:0)

您必须具有Form2的实例,并且您AddHandler要订阅其Closed事件。

我不知道您如何创建Form2。因此,这是一个适合您情况的解决方案。只需将_myForm2 = New Form2()替换为您创建的

Public Class Form1

    Public ReadOnly Property MyForm2 As Form2
        Get
            Static _myForm2 As Form2
            If _myForm2 Is Nothing Then
                _myForm2 = New Form2() ' replace with how your Form2 is created
            End If
            Return _myForm2
        End Get
    End Property

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler MyForm2.FormClosed, AddressOf myForm2_Closed ' add event handler
        MyForm2.Show()
    End Sub

    Private Sub myForm2_Closed(sender As Object, e As FormClosedEventArgs)
        Me.Refresh()
    End Sub

End Class

如果您使用默认的Form2实例,则不应该这样做。但是,如果必须,您可以使用_myForm2 = Form2