Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i use vb studio 2017
when click on button in the form1 then form1 close and form2 show and also when click on button in the form2 then form2 close and form1 show
my code is



but when click button application is close why.

What I have tried:

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

Form2.Show()
Me.Close()

End Sub
Posted
Updated 15-Apr-22 2:31am

1 solution

The problem is that your Application object automatically opens Form1 when your app starts, and when that closes the Application also closes itself - which will close all other forms in the app!

A better solution would be to Hide Form1, then create an instance of Form2, add a handler for the FormClosing Event which unhides the current instance of Form1.

That way, no forms get auto closed, and OOPs principles are still followed - Form2 doesn;t need to know that Form1 exists, as Form1 is responsible what what happens when Form2 is displayed and closed down.
 
Share this answer
 
Comments
Maciej Los 15-Apr-22 14:00pm    
5ed!

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



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