0

I'm a little confused as to how I'm supposed to handle a chain of dialogs in my VB.net application. For example I have 4 forms, each one has a simple label and is subsequently called from the previous one as a dialog.

First form:

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim frm As New Form2
    Form2.ShowDialog(Me)
End Sub
End Class

Second form:

Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim frm As New Form3
    frm.ShowDialog(Me)
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim cls As New LabelFlash
    cls.Flash()
End Sub
End Class

Third form:

Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim frm As New Form4
    frm.ShowDialog(Me)
    Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim cls As New LabelFlash
    cls.Flash()
End Sub
End Class

Fourth form:

Public Class Form4
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim cls As New LabelFlash
    cls.Flash()
End Sub
End Class

And the class they all refer to just toggles the visibility of a label on each form:

Public Class LabelFlash
Sub Flash()
    If Form1.Label1.Visible = False Then
        Form1.Label1.Visible = True
    Else
        Form1.Label1.Visible = False
    End If

    If Form2.Label1.Visible = False Then
        Form2.Label1.Visible = True
    Else
        Form2.Label1.Visible = False
    End If

    If Form3.Label1.Visible = False Then
        Form3.Label1.Visible = True
    Else
        Form3.Label1.Visible = False
    End If

    If Form4.Label1.Visible = False Then
        Form4.Label1.Visible = True
    Else
        Form4.Label1.Visible = False
    End If
End Sub

End Class

Here's my question. When I only have forms 1 and 2 open and I click the button, the label toggle works fine. However, when I get to the third (and fourth) windows only the labels on form 1 and form 2 will toggle and not the third and fourth ones. Additionally, when I close the fourth window it also closes the third at the same time.

I'm sure there's something I'm missing here. Thanks for any help.

evan
  • 43
  • 1
  • 7
  • 1
    Try to understand what is the [Default Form Instance in VB.NET](http://jmcilhinney.blogspot.it/2009/07/vbnet-default-form-instances.html). A very bad design decision that leads to your confusion. See here https://stackoverflow.com/questions/4698538/why-is-there-a-default-instance-of-every-form-in-vb-net-but-not-in-c – Steve Aug 08 '17 at 19:48
  • First, the `FormX.Label1` code in the LabelFlash class is not the same as the form using in the button clicks. The class uses the default instance, the clicks create an explicit (and different) instance object. The clicks should also be using `Using` blocks since dialogs are a resource. Please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Aug 08 '17 at 19:49
  • Many thanks for the help. Using My.Forms.FormX.ShowDialog() seems to do the trick. – evan Aug 08 '17 at 20:16

0 Answers0