I have narrowed my problem to this simple case, but can't seem to find what's going on:
- I have two forms, on with a single button, and the other empty.
- On clicking the button, form1 hides and shows form2
- when showung up, form2 will hide, and show form1 back again
In addition, when entering VisibleChanged, Form2 will stop with a MsgBox
The code follows.
Now the Expected behavior, when clicking on button would be
- Form1 hides
- first MsgBox for visible turning true due to Form1 calling Form2.show
- second MsgBox for visible turning false due to Form2 calling Me.hide
- Form1 shows up
all this does happend, but then,
- Form2 shows up (Form1 is still there)
- a msgbox shows up (telling that form2.visible is True again)
- a msgbox shows up (telling that form2.visible is False now)
- Form2 hides
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 also
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
Me.Hidefrom aVisibleChangedhandler makes the event happen one more time after the original handler returns. That is, callingHidecauses an immediate nestedVisibleChangedlike it should, the second handler exits doing nothing, the control proceeds toForm1.Showand exits the original handler, and thenVisibleChangedhappens again. I'm not sure if it's a bug or something obscure but expected, but if you want it to stop I suggest you callMe.Hidefrom some other event, notVisibleChanged. - GSergForm2that will throw an exception if conditions are not met. - GSerg