开发者

vb.net passing values out of forms

I have a very simple windows forms setup. Form1 has a progress bar and a button on it, when clicked the button opens Form2 which also has a button on it that launches Form3. On Form3 is a button which I want to use to raise an event back to Form1.

To achieve this can I add an event handler on form1 that will listen for an event of the type raised in form3? Or do I have to pass r开发者_运维技巧eferences to form1 to form2 and then from form2 to form3?

Any advice on the best way to achieve this is greatly appreciated.

Many thanks


You'll want to add an event handler on each form that will "bubble-up" the event being thrown on the third form.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜