我的项目使用多种表单来收集信息。由于子表单不能从实例化它们的方法中访问变量,因此我决定创建一个自定义类,该类可以保存子表单中收集的信息,以便在父表单中使用该信息。但是,我不确定何时会处理我的自定义类的实例。
Form1收集信息并使用它来确定下一个要启动的子表单。
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim customClass As New CustomClass
Dim form2 As New Form2(customClass)
form2.Show()
Dim form3 As New Form3(customClass)
form3.Show()
End Sub
End Class
Form2收集数据并将值分配给CustomClass中的共享变量,以及进行多次函数调用。
Public Class Form2
Private _CustomClass As CustomClass
Public Sub New(ByRef instance As CustomClass)
InitializeComponent()
_CustomClass = instance
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
_CustomClass.MySharedVariable = TextBox1.Text
_CustomClass.MyCustomMethod()
Me.Close()
Me.Dispose()
End Sub
End Class
customClass
何时被销毁?它是在End Sub
之后从其父方法处理,还是在使用该实例的所有表单都已关闭后处理?