Solved

Unloading .NET FORMS

Posted on 2004-04-18
12
186 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 20 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regex validation 2 28
EF5: Question about Metadata Artifact Processing 4 22
SqlServer no dupes 25 38
VS 2015 is giving [No relevant source lines] 2 26
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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