Is there a need to close my forms?

joyxl

Member
Joined
Dec 18, 2005
Messages
6
Programming Experience
Beginner
I am a VB.NET newbie working on a simple Pocket PC application.

I used the following code to switch my forms.
In form 1:
Dim Form2 As New Form2
Form2.Show()
Me.Hide()

In form 2:
Dim Form1 As New Form1
Form1.Show()
Me.Hide()

It does what just the way I wanted, but I have read that hiding the form is not disposing or closing it. It will not release resources. I am going to have quite a number of forms and I wonder if there will be any problems(PPC application). From my code, it will create a new instance of a form whenever the user switchs form.

I have tried the code below, but the Form2 just wouldn't appear
Dim Form2 As New Form2
Form2.Show()
Me.Close()
or
Me.Hide()
Me.Close()

Should I add the Close() or Dispose() code? If yes, where should I add it?
What is the differences between Close() or Dispose()?

Thanks a lot :)
 
If you just Hide a form then that form still exists and will be using resources, but you just can't see it on screen. If you call Me.Close on a form then the form will close when the current method completes. If your form is the startup form for the application then closing it will also close the app.

The difference between Close an Dispose is that under certain circumstances Close calls Dispose and under others it doesn't. Close is implemented something like this:
VB.NET:
If Me.Modal Then
    Me.DialogResult = Windows.Forms.DialogResult.Cancel
Else
    Me.Dispose()
End If
 
how to close forms?

So how do i switch forms and at the same time, close forms i have created previously?

thanks
 
joyxl said:
I use Application.Exit() to quit the program.

Does this code closes all the forms created?

Thanks

yes, though i was told by my college vb.net instructor a couple years ago to avoid using the Application.Exit statement becuase if you do use it and you have any app settings that needs to be saved, depending on how the program is written, the save settings sub(s) wont get called with applicaton.exit

if you have a form as a startup object just close that form (Me.Close) and all chile form's are closed as well

if you use Sub Main as the start point, then simply let the program reach End Sub in Sub Main and that closes the application
 
Not to steal the thread but how do I do this:

I have a form2 open a button click events loads form3 with follow up data to form2 and form2 is hidden (visible set to false) then form3 is shown. Now I am finished on form3 and want to unhide (visible set to true) for form2.
 
When you call Application.Exit none of the forms raise a Closing or Closed event, so if you have placed any code in handlers for those events for any form it will not be executed.
GaryMazzone said:
Not to steal the thread but how do I do this:

I have a form2 open a button click events loads form3 with follow up data to form2 and form2 is hidden (visible set to false) then form3 is shown. Now I am finished on form3 and want to unhide (visible set to true) for form2.
I've lost count of the number of times I've answered this questions or variations on the same theme. Forget the fact that these are forms. If you want object A, being of any type, to affect object B, being of any type, then object A needs a reference to object B. In your case form3 is object A and form2 is object B, so form3 needs a reference to form2. If form2 is creating form3 then that is the logical place and time to pass that reference. Either rewrite the form3 constructor to take an argument or else add a public property. form2 can then pass "Me" to the constructor or property and form3 can save that reference to a private member variable. You've already been passing arguments to constructors and setting properties to pass data to objects before. This is no different.
 
hehe, good one jmcilhinney ! :) should be made sticky both here at Forms and also at Components...
 
Back
Top