Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1457
  • Last Modified:

Best way to close a form and load another

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
sunnycoder
Asked:
sunnycoder
  • 12
  • 7
  • 3
  • +1
2 Solutions
 
jpaulinoCommented:
In My Project - Application Tab on the ShutDown Mode select "When Last Form Closes"
0
 
sunnycoderAuthor Commented:
Already done.
0
 
BinuthCommented:
>>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
Industry Leaders: 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!

 
sunnycoderAuthor Commented:
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
 
jpaulinoCommented:

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

And not:
Me.Close()
NewForm.Show()
 
0
 
sunnycoderAuthor Commented:
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
 
BinuthCommented:
>>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
 
sunnycoderAuthor Commented:
No, it was not. MainAssistant is the name of the class not an instance.
0
 
jpaulinoCommented:
HAve you created a new instance of the form ?
0
 
sunnycoderAuthor Commented:
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
 
BinuthCommented:
>>"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
 
sunnycoderAuthor Commented:
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
 
sunnycoderAuthor Commented:
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
 
jpaulinoCommented:
But you in the FormClosed event are ending the application !!!
That way will never work!
0
 
sunnycoderAuthor Commented:
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
 
jpaulinoCommented:
You can set a boolean variable to true when you click the ToolStripMenuItem and check the state on the FormClosed event
0
 
sunnycoderAuthor Commented:
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
 
jpaulinoCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"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
 
sunnycoderAuthor Commented:
Thanks a ton!!
0
 
sunnycoderAuthor Commented:
NewForm.Show()
Me.Close()


Me.Close()
NewForm.Show()

Just to confirm - Are the above two code segments identical?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
jpaulinoCommented:
>> 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
 
sunnycoderAuthor Commented:
Food for thought :)
Me.Close()
NewForm.Show()
NewForm pops up before application exits  .... thanks guys you have been of immense help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 12
  • 7
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now