Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Unloading .NET FORMS

Posted on 2004-04-18
12
Medium Priority
?
194 Views
Last Modified: 2010-04-24
On The Project Start up i am loading a form. On Button Click i want to unload this form and want to load another form. What is the method to unload the current form
0
Comment
Question by:ramkish18
  • 5
  • 4
12 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 80 total points
ID: 10856966
0
 

Expert Comment

by:smlewisv
ID: 10860802
Me.Close()
Using this will close the form and all the resources created within the form are also closed.

Me.Finalize()
Performs some cleanup operations before the objects is cleand by the garbage collector.

It all depends on what you have done before closing the form.

Hope this helps



0
 
LVL 14

Expert Comment

by:ptakja
ID: 10877958
If you close the startup form, your application will quit.  What you need to do is add a Module that has a Sub Main procedure in it;   like this:

In the Main procedure, add the code to instantiate and run your first form:


Module Main
   Public Sub Main()
      Dim frm1 as New Form1
      frm1.Show()
      frm1.Dispose

      Dim frm2 as New Form2



   End Sub
End Modyule
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Expert Comment

by:ptakja
ID: 10877993
Crap....I hit the wrong button.  Here's the rest of the code:

Module Main
   Public Sub Main()
      Dim frm1 as New Form1
      frm1.Show()
      frm1.Dispose

      Dim frm2 as New Form2
      frm2.Show()
      frm2.Dispose

   End Sub
End Module

The Dispose method is used to destroy the form.  It won't actually be removed from memory until the Garbage Collector runs again.  The .NET Framework decides when to run the collector.  To force it to run you can call GC.Collect(), although this is not recommended.
0
 

Expert Comment

by:smlewisv
ID: 10881632
ptakja,

if you dispose the form after showing it, the form will be visible only for a very short time (almost 0) so you won't be able to do anything with the form. Instead of just showing the form you can use ShowDialog.
0
 
LVL 14

Expert Comment

by:ptakja
ID: 10881667
smlewisv,

You are correct.  An oversight in my example.  Thanks for pointing that out.

Here's the corrected code.

Module Main
   Public Sub Main()
      Dim frm1 as New Form1
      frm1.ShowDialog()
      frm1.Dispose

      Dim frm2 as New Form2
      frm2.ShowDialog()
      frm2.Dispose

   End Sub
End Module
0
 
LVL 14

Expert Comment

by:ptakja
ID: 10881702
Forgot one other important item, after you create the Main procedure, you need to go into Project --> <ProjectName> Properties.  Under the Common Properties category and General subcategory, you want to set the Startup Object to be "Sub Main".
0
 

Expert Comment

by:smlewisv
ID: 10883957
no problem, ptakja.

One more thing. I don't know if this is the best way to run an application from within the sub Main.

I usually use as follows:

Public Sub Main()
   Application.Run(new Form1)
end sub

BUT, I haven't read anywhere that this or that is the best or the correct way to do it.

regards
0
 
LVL 14

Expert Comment

by:ptakja
ID: 10888828
I have done both.  The issue with the Application.Run is that when Form 1 closes, the app will shut down, which is not what you desire.  Although you could probably execute it again with Form2.  I've never tried that though.
0
 

Expert Comment

by:smlewisv
ID: 10890330
Indeed, Application.Run is to be used with the main form, not with other.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question