Solved

Unloading .NET FORMS

Posted on 2004-04-18
12
190 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

636 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