Closing Form1 and Opening Form2

Tyecom

Well-known member
Joined
Aug 8, 2007
Messages
78
Programming Experience
Beginner
I have a "Exit" button on Form1. When I click this button I would like to close the existing Form and go to Form2. I have the following code in the button's click event, but it only closes the existing form, but do not go to Form2.

Private Sub Exitbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReturnToMainbtn.Click

Me.Close()
mainMenu.Show()

End Sub

Is theres something wrong with this code? Thanks in advance
 
is ur form 2 already open?
 
Check your Shutdown mode in project properties, Application tab.
 
Where is mainMenu instantiated? Do you have your Form open as Modal? If mainMenu (Form2) is the main one, and Form is a dependent one, open Form as Modal from Form2, and you automatically will return to Form2 when Form closes.

Example, using the name mainMenu for Form2, and childForm for Form:

Private Sub mySubIn_mainMenu

Dim childForm as Form

Try

'Open child form as modal.

childForm = New Form
childForm.ShowDialog()

Catch ex as Exception
Throw ex

Finally
If Not childForm Is Nothing Then
childForm.close()
childForm.Dispose()
childForm = Nothing
End If

End Try

End Sub
 
Try to change it

Private Sub Exitbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReturnToMainbtn.Click

mainMenu.Show()
Me.Close()


End Sub

show the other form first, otherwise it will never show the "other form". because your form already closed before it executed open "other form" command :)
 
as JohnH said: "Check your Shutdown mode in project properties, Application tab.", otherwise application will shutdown after closing "form1" (or whatever the name is, the form u started ur application with). After that, you can use on your button:

form_name.show
me.close



- Mario
 
Back
Top