Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple one here

Status
Not open for further replies.

shannanl

IS-IT--Management
Apr 24, 2003
1,071
US
I am moving from VB6 to VB.NET and am playing around. The first thing I have ran into is showing and unloading forms.

I used to use this code in a command button:

form2.show
unload form1

...and then I would use (set form1 = nothing) in the unload event

Obviously this does not work anymore. How do I show a form and unload a form? P.S. - I have a book to read but I am just playing around now.

Thanks,

Shannan
 
Hey Rick,
Say you have 2 forms. Form1 has been opened and has a button that points to Form2. All I need to do is close Form1 and Show Form2 when this button is clicked. I've been able to Show Form2 without any problem but I can't close Form1. I'm also just moving from VB6 where this was a very simple task. In VB.net I'm not having much luck with the close command. Any help you could offer would be very much appreciated.
Thanks
 
If Form1 is your startup object, then when it closes the application will end. so if on a button click you have:

Code:
dim frm2 as new Form2
frm2.show
me.close

frm2.show will fire in a new thread. at the same time frm1 (me) will close. Closing the startup objected ends the primary thread and the application will end.

One way arround this is to use ShowDialog. ShowDialog will show the new form but halt the primary thread until the form that was opened with Showdialog quits.

Code:
dim frm2 as new Form2
frm2.showdialog
me.close
[code]

This will keep the first window open, but the user will only be able to interact with the 2nd window.

-Rick

----------------------
[URL unfurl="true"]http://www.ringdev.com[/URL]
 
Use this code to keep the same form from opening twice.

Private frm2 As Form2

Private Sub Button1_Click(ByVal Sender As Object, ByVal e as EventArgs) Handles Button1.Click
If IsNothing(frm2) = False Then
'We have to check if its Nothing, If it is and you try to "Show()" it you will get an error
If frm2.IsDisposed = False Then
'The form can be "Something" (not nothing) but be disposed. Calling the show method on a disposed form will cause an error.
'We dont do anything here because the form is not nothing, and its not disposed so it must be open
Else 'The form isnt nothing but it is disposed.
frm2 = New Form2()
frm2.Show()
End If
Else 'The form is nothing
frm2 = New Form2()
frm2.Show()
End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top