BaeFell
BaeFell

Reputation: 650

How to open a new form but closing the old one in VB

I have a welcome to my application as it loads up, but then need to have that form close and login form open when the continue button is hit.

My code:

    Me.Close()
    Dim Login As New Form
    Login.Show()

When I click the button it only closes the welcome form and then ends the application. If you can help thanks! :)

Upvotes: 14

Views: 107775

Answers (8)

Daniel
Daniel

Reputation: 1

If you close sub main form from application, your application will close. However, you can close and open other forms if they are not the sub main form. Maybe you can just hide it instead.

Upvotes: 0

Filip
Filip

Reputation: 1

You just need to put Hide() instead of Close :)

So for example, in the project im doing right now...

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click // Button1.Click is your "continue" button
        Hide()
        LogInFrom.Show()
    End Sub

Upvotes: -2

BullfrogIIII
BullfrogIIII

Reputation: 1

Try this..

On your welcome form when closing:

Me.hide()
Dim Login As New Form
Login.Show()

On your login form when in loading event:

Private Sub Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    WelcomeForm.Close()

End Sub

This will try to hide the first form and load the second form. And when second form is completely loaded it will try to close the first form.

Make sure that on your Application Tab under your Project properties the option is set to "When last form closes".

Upvotes: 0

Zigab123
Zigab123

Reputation: 51

Better is if you use Me.Hide()

Upvotes: 5

Rahul Tripathi
Rahul Tripathi

Reputation: 172588

You can set the properties of the project to select "When last form closes" in the shutdown mode dropdown

Update:-

"Project" menu -> 'YourApp' Properties... -> Application Tab

find : "Shutdown Mode"

Change from

"When startup form closes" --> "When last form closes"

Upvotes: 23

Matt Wilko
Matt Wilko

Reputation: 27342

There is a shutdown mode project property. This controls the application lifecycle.

Make sure you set this to "When last form closes"

Then your code should work as you expect.

What is happening is that you have that setting set to shutdown "when startup form closes", so by doing Me.Close on the startup form, this shuts down the application, all code after this line is effectively ignored.

Upvotes: 4

Alex
Alex

Reputation: 4948

If your Welcome Form isn't your main form, you just need to put your Me.Close after your Login.Show()

Dim Login As New Form
Login.Show()
Me.Close()

Upvotes: 2

Markjw2
Markjw2

Reputation: 151

show the form before the close.

Dim Login As New Form
Login.Show()
Me.Close()

Upvotes: 9

Related Questions