Google Answers Logo
View Question
 
Q: removing VB.NET form from memory ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: removing VB.NET form from memory
Category: Computers > Programming
Asked by: jthoma1-ga
List Price: $5.00
Posted: 16 Jan 2003 11:59 PST
Expires: 15 Feb 2003 11:59 PST
Question ID: 144327
I am trying to design an application in VB.NET that starts with
allowing a user to select a profile/login from a small login window,
and once they login, it opens up the main form (maximized) and closes
the login form.  The problem is when you close/terminate the main
form, the application doesn't unload from memory, because the login
form is still loaded.  When the user logs in on the login form, I
create a new instance of the main form, and turn the visible property
to false on the login form.  But when I close the main form, I still
have an invisble login form in memory.  I tried to dispose or close
the login form when the user logs in, but it also closes the main form
(as it trys to open). What is the best way to address this problem. 
When the user closes the main form, I want the entire program to be
removed from memory.
Answer  
Subject: Re: removing VB.NET form from memory
Answered By: cerebrate-ga on 16 Jan 2003 17:41 PST
Rated:5 out of 5 stars
 
Dear jthoma-ga,

This is an interesting one, because VB.NET to some extent conceals
what's "really" going on from you. If you were to compare a VB.NET
Windows Forms application to a C# one, you'd see that in the C#
application there is a method in the main form which looks like this:

public static void Main ()
{
  Application.Run (new Form1 ()) ;
}

Application.Run () is the method that actually controls any Windows
Forms application - and when a form is passed to it, it runs for only
as long as that form is open. When it is closed, Application.Run ()
exits, and so, given the Main method above, so does the application.

VB.NET, although it doesn't show it in the code, actually does have
this same method - it's quietly inserted for you into whatever form
you have specified as the startup object in the Project Properties
dialog box - so whenever that form is closed, your application
automatically exits.

The best way to solve this in applications like yours, I have found,
is to set the startup object in the Project Properties dialog box to
"Sub Main". This restores the C#-style behaviour of requiring an
explicit "Main" method somewhere in the application. Then, add a
method to the main form which looks like this:

Public Shared Sub Main()
    Dim f1 As Form1

    f1 = New Form1
    f1.Show()
    Application.Run()
End Sub

where f1 is the form which you wish to be shown when the application
starts, in your case, the login form. You can then open other forms
from this first form and close it, and the application will continue
to run.

The thing to note here is that the Application.Run() method now has
*no* form specified. When this is the case, the application will run
forever regardless of whether any forms at all or open - in short,
we've just created an application with no way to exit, yet.

When you use this technique, you need to provide an explicit statement
of when you wish to exit the application, by calling the
Application.Exit() method. If you wish this to happen when the main
form is closed, for example, you need to add a handler for the
"Closed" event to the main form:

Private Sub Form1_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed
   Application.Exit()
End Sub

which will call Application.Exit () at the appropriate time. Using the
Closed event for this saves duplicating code in various handlers for
menus, the close box, and so forth.

Another possible technique to achieve this is to set the startup
object in the Project Properties dialog to your main form, configure
that form to start as invisible, and then show your login form from it
- making it visible again and closing the login form when the login is
complete. This solves the problem in this specific case, as the main
form is now the one which will cause the application to terminate on
its being closed, but this has the disadvantage of only being
applicable to the two-form case and would require considerable
rewriting should additional "main" forms be added in the future. As
the solution I outline above is more generalisable and upgradable, I
consider it superior.

If this answer isn't quite what you're looking for, please feel free
to request a clarification, 
    
cerebrate-ga

Search strategy:

Personal knowledge & Visual Studio .NET library.
jthoma1-ga rated this answer:5 out of 5 stars
An exceptional answer, as usual! I used the first, more generalisable
solution. Thanks again!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy