Solved

Best way to close a form and load another

Posted on 2008-10-11
24
1,446 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
  • 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
 
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
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

 
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 85

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 85

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
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…

706 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

19 Experts available now in Live!

Get 1:1 Help Now