0
votes

Please, I am having issues and needs help with the following code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
   Me.Close()  'Form2
   Form3.showDialog()
End Sub

Report:

I have Form2 and Form3. With the above code, I intend to close Form2 and open Form3 (on button click). When I implemented the code, Form3 popped-up as expected but surprisingly few seconds after, the closed Form2 also popped-up. I believe "Me.Close" serves primarily to kill Form2 permanently. However, when I go to Project menu and set the Startup Form to start from Form2, everything works fine. But this would ignore Form1 which is the Welcome Page.

I have attempted using Me.Visible = False, dispose, and Me.Hide in place of “Me.Close” but all to no avail. I have also tried re-writing the code such that Form3.ShowDialog is written before Me.Close. I have also attempted using Form3.Show in place of "Form3.ShowDialog". Meanwhile, I also have set my Application Tab under Project properties to "When last form closes".‎ Still, the closed Form still re-occurring.

The following is the code in Form1, could this be the cause?

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer1.Interval = 10000
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Form2.Show()
        Me.Hide()
    End Sub

End Class

I am using Visual Studio 2015.

1
You have code somewhere else popping up a Form2. What happens if you comment out the Form3.ShowDialog() line? - LarsTech
I'll go back to You have code somewhere else popping up a Form2 - LarsTech
You should put code in your post, not in comments. Also, see Winform Splash Screen - VB.NET - Timer - LarsTech
The timer keeps ticking, Me.Hide() will not stop it. So it displays Form2 again. You'd strongly favor using Me.Close() instead, along with Project > Properties > Application tab, Shutdown mode = "When last form closes". Or add Timer1.Enabled = False - Hans Passant
@Wilfred Did you try stopping the timer by putting Timer1.Stop() as the first line of the Timer1_Tick sub? - Andrew Morton

1 Answers

-1
votes

I think, the problem is the method ShowDialog. This will open the new Window in modal mode, which means only the new form is accessible, but when you close Form3 the focus will get back to Form2. Therefore is has to be there. Maybe you could just try:

Form3.Show()