Rattle
Rattle

Reputation: 23

Visual Basic:Closing and Opening Forms?

I am making a program in which you can press a button and it makes a new form but I have a little bit of a problem.

Because I have Form1 and Form2. When I press the button on Form1 it shows Form2 and it should just close Form1.

Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        Form2.Show()
        Me.Close()

What actually happens is that it closes Form1 and Form2 even if I said Me.Close().

Is there a fix to it or did I just do it wrong somehow?

Upvotes: 2

Views: 144

Answers (1)

cyboashu
cyboashu

Reputation: 10443

I bet Form1 is your startup form. If not sure sure check that in Project properties. enter image description here

The moment you close Form1, your application terminates altogether. So in simple words you can't close Form1 and show another form, at least not with 2 lines of code.

What you can do is hide Form1 and Show Form2.

  Form2.Show()
    Me.Hide()

Now when you close Form2, make sure you either unhide Form1 (so that usercan manually close it) or automatically close Form1 from Form2's FormClosing event, else your process will be alive in the background, a ghost :)

So in your Form2, add the FormClosing event handler and then inside that close Form1

 Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing      
        Form1.Close()
    End Sub

Upvotes: 2

Related Questions