Solved

Best way to close a form and load another

Posted on 2008-10-11
24
1,454 Views
Last Modified: 2010-04-21
1.
My application consists of multiple forms. What is the best way to unload one form and load another one?

I know about hide() but hide does not release the resources. Is there a way to tell the run time environment that this form can be unloaded and resources freed.

2.
In one form I have
        If res = True Then
            MainAssistant.Show()
            Me.Close()
        Else
This works fine ... when the condition is true, current form is closed and MainAssistant shows up.

In MainAssistant I have
    Private Sub AddToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddToolStripMenuItem.Click
        Me.Close()
        NewForm.Show()
    End Sub
When this menu item is clicked, NewForm shows up momentarily and then closes. Why? The code at both the places is the same.
0
Comment
Question by:sunnycoder
[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
  • 12
  • 7
  • 3
  • +1
24 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 22693514
In My Project - Application Tab on the ShutDown Mode select "When Last Form Closes"
0
 
LVL 45

Author Comment

by:sunnycoder
ID: 22693517
Already done.
0
 
LVL 14

Expert Comment

by:Binuth
ID: 22693520
>>In one form I have
>>       If res = True Then
>>            MainAssistant.Show()
>>            Me.Close()
>>       Else
>>This works fine ...

          Is MainAssistant declared in same class or outside class(globally) ?


>>Private Sub AddToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As >>System.EventArgs) Handles AddToolStripMenuItem.Click
>>        Me.Close()
>>       NewForm.Show()
>>    End Sub


      Is NewForm declared in same class  ?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 45

Author Comment

by:sunnycoder
ID: 22693523
I made all the forms using designer. Add->New Item.
So I guess it should be all globally visible public classes - is that the default behavior?
Had it been class visibility issue shouldnt I have encountered build errors?
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22693526

But you should do:
NewForm.Show()
Me.Close()

And not:
Me.Close()
NewForm.Show()
 
0
 
LVL 45

Author Comment

by:sunnycoder
ID: 22693529
jpaulino,

I tried both orders - same result. Does the order really matter? Its just telling run time env to load another form and unload it. I would expect unloading to begin after the execution function has exited. Is that not the case? Sorry - I am just a beginner in VB.NET
0
 
LVL 14

Expert Comment

by:Binuth
ID: 22693539
>>I made all the forms using designer. Add->New Item.
>>So I guess it should be all globally visible public classes - is that the default behavior?
>>Had it been class visibility issue shouldnt I have encountered build errors?

no.., i asked about , where MainAssistant object initialized with new keyword
(dim MainAssistant as new ....)



0
 
LVL 45

Author Comment

by:sunnycoder
ID: 22693544
No, it was not. MainAssistant is the name of the class not an instance.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22693547
HAve you created a new instance of the form ?
0
 
LVL 45

Author Comment

by:sunnycoder
ID: 22693557
I have a LoginForm specified as my first form. When a specific condition is met, I simply call MainAssistant.Show() where MainAssistant is another class ... Likewise on selecting a specific menu item, I make a call to NewForm.Show() - again NewForm is class name - I did not instantiate any objects of these classes.
0
 
LVL 14

Expert Comment

by:Binuth
ID: 22693568
>>"I simply call MainAssistant.Show() where MainAssistant is another class ..."

did you write any code in MainAssistant >> Show()   method ?, pls post the code of Show()  method or provide full class code ...
0
 
LVL 45

Author Comment

by:sunnycoder
ID: 22693574
Public Class MainAssistant
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

    Private Sub AddPatientToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddPatientToolStripMenuItem.Click
        NewPatient.Show()
        Me.Hide()
    End Sub

    Private Sub MainAssistant_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Application.Exit()
    End Sub
End Class
0
 
LVL 45

Author Comment

by:sunnycoder
ID: 22693801
Now that I think of it, it indeed seems a bit weird that forms are being loaded without me ever instantiating them at all. Whats going on? Is run time env automatically instantiating them? If yes then what are the rules/criteria for doing so? When would RTE create an instance and when it wont?
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 300 total points
ID: 22693980
But you in the FormClosed event are ending the application !!!
That way will never work!
0
 
LVL 45

Author Comment

by:sunnycoder
ID: 22693995
okay .. I get the point ... but how do I handle that when user clicks on the close button (the x mark on top right) the application should exit - or would that be handled automatically?
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22694010
You can set a boolean variable to true when you click the ToolStripMenuItem and check the state on the FormClosed event
0
 
LVL 45

Author Comment

by:sunnycoder
ID: 22694018
Makes perfect sense ... I would do that. Final question http:#22693801 .. I did not instantiate the forms - I guess RTE did - when? what is the criteria for auto instantiation?
Thanks!!
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22694027
I always create a new instance. If you dispose the form then you will get an error if you don't create a new instance.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 200 total points
ID: 22694149
"what is the criteria for auto instantiation?"

You are using what are known as the "default instances" of the Forms.  This feature was available back in the old VB6 days but disappeared with the first versions of VB.Net.  Then it was brought back in VB.Net 2005 and above.

The default instance is accessed by simply using the Forms name.  You can do this from ANYWHERE in your project.  The default instance of a Form acts exactly like the singleton pattern does...you can only have ONE default instance.

Default Instances of Forms:
http://msdn.microsoft.com/en-us/library/ms233839.aspx
http://msdn.microsoft.com/en-us/library/87y2hdsf.aspx

    "You can access the forms provided by the My.Forms object by using the name of the form, without qualification. Because the property name is the same as the form's type name, this allows you to access a form as if it had a default instance. For example, My.Forms.Form1.Show is equivalent to Form1.Show."

Singleton:
http://msdn.microsoft.com/en-us/library/ms998558.aspx
0
 
LVL 45

Author Closing Comment

by:sunnycoder
ID: 31505286
Thanks a ton!!
0
 
LVL 45

Author Comment

by:sunnycoder
ID: 22694181
NewForm.Show()
Me.Close()


Me.Close()
NewForm.Show()

Just to confirm - Are the above two code segments identical?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22694197
No they are NOT...

Even with the  "Shutdown Mode" set to "When last form closes", putting "Me.Close()" first will result in the application closing.

At least that is the behaviour I saw with VB.Net 2005 Express on a WinXP Pro machine.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22694221
>> At least that is the behaviour I saw with VB.Net 2005 Express on a WinXP Pro machine.
Idle_Mind is obviously right. When you do Me.Close() it closes the form (and in this case the application).
0
 
LVL 45

Author Comment

by:sunnycoder
ID: 22694316
Food for thought :)
Me.Close()
NewForm.Show()
NewForm pops up before application exits  .... thanks guys you have been of immense help.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

624 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