Vb appears again without prompting

I have narrowed down my problem in this simple case, but cannot find what is going on:

  • I have two forms, one button and the other blank.
  • On button click, form1 hides and shows form2
  • when showung up, form2 will hide and show form1 back

Also, when you type VisibleChanged

Form2, the MsgBox will stop using the code that follows.

Now the expected behavior on button click will be

  • Hidden forms1
  • first MsgBox for visible rotation true because of Form1 calling Form2.show
  • second MsgBox for visible rotation false due to Form2 calling Me.hide
  • Form 1 will appear

it all happens but then

  • Form2 appears (Form1 is still there)
  • msgbox appears (saying form2.visible is True again)
  • msgbox appears (says form2.visible is now inactive)
  • Hiding form2

Any idea why?

here's the code:

Public Class Form1
    Private Sub ButtonGO_Click(sender As Object, e As EventArgs) Handles ButtonGO.Click
        Me.Hide()
        Form2.Show()
    End Sub
End Class

      

and

Public Class Form2
    Dim calls As Integer = 0
    Private Sub Form2_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
        calls += 1
        MsgBox("calling : " & calls & " / Me.Visible : " & Me.Visible)
        If Me.Visible Then
            Me.Hide()
            Form1.Show()
        End If
    End Sub
End Class

      

+3


source to share


1 answer


Ok, so to close: thanks for your answers, they tell you what to do for a "complete" VB coder.

As far as my students, i.e. people who are just using drag-n-drop-VB, the solution is to check the conditions in Form1 and then only call Form2 when it appears.



Note. This may sound trivial, but it may be incompatible with the idea of ​​"encapsulation". This is what posed this problem in the first place in my case.

0


source







All Articles