Click to See Complete Forum and Search --> : unload a form which is hiden


jayson_13
August 8th, 2002, 09:48 PM
just simple question.
in my form1, after user login it hides and load the other form.
but after the program exit .. it's still not ended because of the hiden form.... what can i do to unload the form?

my code(in form1):

dim objfrmform2 as new form2
me.hide
objfrmform2.show

how do i unload the form1?
is there better way to load another form just like vb6?
because everytime when form2 exited.. i have to declare an form 2 object .. thank you!

sxcostanzo
August 9th, 2002, 10:08 AM
There are two ways to handle this.

The first is to create a collection of forms so you can loop through that collection and close the forms as necessary. The advantage to this is that when form1 closes it does not close form2. The form would stay active as long as the collection was active.

The second is to add a parameter to the New function of form2:

Public Sub New(ByVal fBaseForm As Object)
MyBase.New()
mBaseForm = fBaseForm
' This call is required by the Windows Form Designer.
InitializeComponent()
End Sub

Where you have declared mBaseForm as a module level object variable.

Your call to create form2 changes to:

New Form2(Me)

so that the reference to form1 is passed to form2.

Finally, add the following closing method:

Private Sub form2_closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
if not mBaseForm is nothing then mBaseForm.close()
End Sub

When form2 closes, it uses the reference to mBaseForm to close the original.

Hope this helps