vb.net从表单中传递值

时间:2011-09-08 08:59:18

标签: vb.net winforms forms events

我有一个非常简单的Windows窗体设置。 Form1上有一个进度条和一个按钮,当单击按钮打开Form2时,它上面还有一个启动Form3的按钮。在Form3上是一个按钮,我想用它将事件提升回Form1。

为了实现这一点,我可以在form1上添加一个事件处理程序,它将侦听在form3中引发的类型的事件吗?或者我是否必须将对form1的引用传递给form2,然后从form2传递给form3?

非常感谢任何关于实现这一目标的最佳方法的建议。

非常感谢

1 个答案:

答案 0 :(得分:0)

您需要在每个表单上添加一个事件处理程序,它将“冒泡”在第三个表单上抛出的事件。

Public Class Form1

    Private WithEvents form2 As New Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        form2.Show()
    End Sub

    Private Sub Form2_MyEvent() Handles form2.MyEvent
        MessageBox.Show("We're back on Form1.")
    End Sub

End Class

Public Class Form2

    Private WithEvents form3 As New Form3
    Public Event MyEvent()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        form3.Show()
    End Sub

    Private Sub Form3_MyEvent() Handles form3.MyEvent
        RaiseEvent MyEvent()
    End Sub

End Class

Public Class Form3

    Public Event MyEvent()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RaiseEvent MyEvent()
    End Sub

End Class