Solved

Best way to close a form and load another

Posted on 2008-10-11
24
1,450 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
abstract class C# 1 31
C#.net split string and then check appropriate checkboxlist 4 27
Need syntax help building hyperlink 2 27
Code enhancement 4 22
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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 …

821 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