Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to close multiple forms at once (Including the one I am on) and then open a new form all in one button click. I have been using this to get to the new form but it won't close the form that I am on. If I recall Form1 then it will close it but it won't close it right before it opens Form2.

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub

Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
Form2.ShowDialog()
Me.Close()
End Sub
End Class

What I have tried:

I have tried Me.Close() and Me.Hide()
Posted
Updated 29-Aug-18 4:51am
v2

You can try this trick, hide the parent first because .Close will close all the instance,then create an instance of the new form, show it, then close the parent form or leave it hidden.

VB
Me.Hide()
        Dim frm2 As New Form2
        frm2.ShowDialog()
        Me.Close()



Hiding and showing forms in C#[^]
 
Share this answer
 
ShowDialog is a "special" way to open a form - it opens the form and does not return until that form has been closed (this is called a Modal Dialog). It's intended for things like a "Save file" dialog which gets the user to give a file name because the save code can't continue until the user has told it where to save the data!

It's not the only way to open a form: there is also the Show method which starts the process of opening the form and returns immediately so your code can continue.

So if you want to open a form and close the existing, you can do it very easily:
VB
Dim f as New Form2
f.Show()
Close()
But ... be aware that if you close the "original" form - the one that starts the application - then your app will immediately terminate and all forms will be closed.
Depending on what you are trying to achieve with this, that may not be what you want to do.
 
Share this answer
 
Form2.Show
Me.Close

if you use Me (the form you are on) as the main form in the settings use:

Form2.Show
Me.hide
 
Share this answer
 
Comments
Richard Deeming 30-Aug-18 10:48am    
Which is what the accepted solution already said.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900