Solved

Unloading .NET FORMS

Posted on 2004-04-18
12
180 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now