Solved

Unloading .NET FORMS

Posted on 2004-04-18
12
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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