Reputation: 5
how do i close my form after opening the next form (VB.net Windows form) like the Next button
I tried Form2.show()
it shows form2 but does not close the form1 and if i type me.close()
, the entire project stops
Upvotes: 1
Views: 1422
Reputation: 1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
AddHandler frm2.FormClosed, AddressOf Form2Closing
frm2.Show()
Me.Hide()
End Sub
Private Sub Form2Closing(sender As Object, e As FormClosedEventArgs)
Me.Show()
RemoveHandler DirectCast(sender, Form2).FormClosed, AddressOf Form2Closing
End Sub
Upvotes: 0
Reputation: 26424
You better not close your form after opening another one from it, unless you want that other one also closed. Otherwise it will cause ownership and visibility side effects, which you really don't want to deal with in the long run.
This is exactly why me.close()
on your main form stops your project. You just have consider paradigms Microsoft put into a Winforms application. If you don't, you are guaranteed to get in trouble.
Instead, a wizard control is what you are probably looking for.
Upvotes: 0
Reputation: 54532
If you just want Form2
to be Visible you can hide Form1
when you show Form2
, then show it Form1
again when you close Form2
. What is happening is that once you close Form1
your program will exit see below edit.
Something like this.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
AddHandler frm2.FormClosed, AddressOf Form2Closing
frm2.Show()
Me.Hide()
End Sub
Private Sub Form2Closing(sender As Object, e As FormClosedEventArgs)
Me.Show()
RemoveHandler DirectCast(sender, Form2).FormClosed, AddressOf Form2Closing
End Sub
If you just are wanting to Close Form1
and not go back to it once Form2
is open, you can change your project settings from When startup form closes(which is the default) to When last form closes then you can close your first form without closing your application.
Upvotes: 4