By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,496 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,496 IT Pros & Developers. It's quick & easy.

how to close and show another form??

P: n/a
Hi,

I have my main form1 and I want to close it and then show form2, in VB6 I
used the following code...

unload me
form2.show

in VB.net I got this far

me.close()
form2.show

as it turned out the code does not work! what's the secret in getting it
work?

thanks,

Gary

Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Gary,

Based on experience, windows application will run the Form1(the form
created when you create a windows application) as the main form
When the Form1 closed, ExitThread will be called to clean up the
application. So when you call the form1.close, the application will exit.
If you want to simulate the behavior of VB6, you may try to code below.

Right click on the project in the Solution explorer, and select properties.
Navigate to Common properties/General/Startup Object, set the value to Sub
Main.

Add a module to the project, copy and paste the code[Module1.vb] below to
the module.
Press F5 to run the application too see if this does the job for you.

[Module1.vb]
Imports System.Windows.Forms

Public Module MyApplication
Public formCount As Integer
Public fm1 As Form1
Public fm2 As Form2
Public Sub OnFormClosed(ByVal sender As Object, ByVal e As EventArgs)
formCount = formCount - 1
If (formCount = 0) Then
Application.ExitThread()
End If
End Sub
Public Sub Main()
formCount = 0
fm1 = New Form1
formCount = formCount + 1
fm2 = New Form2
formCount = formCount + 1
AddHandler fm1.Closed, AddressOf OnFormClosed
AddHandler fm2.Closed, AddressOf OnFormClosed
fm1.Show()
Application.Run()
End Sub
End Module

[Form1.vb]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Close()
fm2.Show()
'Close form1 and show form2
End Sub

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #2

P: n/a
* "Gary" <sp**@spam.com> scripsit:
I have my main form1 and I want to close it and then show form2, in VB6 I
used the following code...

unload me
form2.show

in VB.net I got this far

me.close()
form2.show

as it turned out the code does not work! what's the secret in getting it
work?


See:

<http://www.google.com/groups?selm=u%23xQOutWDHA.2100%40TK2MSFTNGP11.phx. gbl>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

P: n/a
The problem is that in vb.net all forms are classes so you have to declare
an object
Dim myForm2 as New Form2

and after you can use your code
myForm2.Show()
Me.Close()
Regards
Valeria

"Gary" <sp**@spam.com> ha scritto nel messaggio
news:O8**************@TK2MSFTNGP12.phx.gbl...
Hi,

I have my main form1 and I want to close it and then show form2, in VB6 I
used the following code...

unload me
form2.show

in VB.net I got this far

me.close()
form2.show

as it turned out the code does not work! what's the secret in getting it
work?

thanks,

Gary


Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.