[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

Application won't shut down

Have been working all night on a simple little program that uses 5 forms (more to be added).  Each form has buttons to open any of the others while closing itself.  Form1 is the startup Form and is hidden instead of closed with these buttons (Form1 is then "visible=true" when re-accessing this form.

The problem is...when closing Form1, the app doesn't shut down.  I have to manually stop it.  Driving me crazy!

(VB 5 & 6 were sooooo much easier!) - VB (all versions) novice here.

I'm guessing it has to do with different instances of Form1?  Assuming that when I hit the button on Form2 to show Form1 and close Form2, it's actually creating/showing a new instance of Form1 (???), and the original Form1 is still running hidden?

How can I reference instances of forms already running - hidden or not - without creating new instances?

Need some pretty low-brow explanations here...

Thanks much for any help.
0
CThomp2005
Asked:
CThomp2005
3 Solutions
 
AlexFMCommented:
Yes, if you create new instance of Form1, this happens. Application continues to run all time when original Form1 instance is active.
You need to hide/show the only Form1 instance. To do this you can use public shared property which gets access to this form instance:

Public Class Form1

    Shared mainFormInstance As Form

    Public Shared ReadOnly Property MainForm() As Form
        Get
            Return mainFormInstance
        End Get
    End Property


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mainFormInstance = Me
    End Sub

End Class

To hide main form from any place of the program write:

Form1.MainForm.Visible = False
0
 
Praveen KumarArchitectCommented:
Here is a simple steps to declare and access forms Globally....

Step1: Create All form variables as Public in a Module
Ex: Public frm as frmInvoice

Step2: Use following code to open a form


'If Instance still exists.....(i.e it's not nothing)
If Not IsNothing(frm) Then
         'and if it has't been disposed yet
         If Not frm.IsDisposed Then
                 'then it must be already instanciated - may be its minimized or hidden
                 frm.WindowState=FormWindowState.Normal
                 frm.BringToFront()
         Else
                'else it has already been disposed, so you can instanciate a new form and show it
                frm = New frmInvoice
                frm.Show()
          End If
Else
             'else the form=nothing, so you can safely instanciate a new form and show it
             frm = New frmInvoice
             frm.Show()
End if  
         
 
0
 
hiteshgupta1Commented:
u r hiding the form using formname.visible method
if u use form.show method than it will create an ew instance of the form
so rather than doing that u should do
formname.visible=true
0
 
CThomp2005Author Commented:
Thanks for all the great input.  As this is my first endeavor using .NET, I find it a but "over my head" (and will eventually revert back to my good old VB5).  I haven't quite "translated" all the code provided into something I understand.  In the end, I've managed to pull off what I'm trying to do in this way.

Form1 - loads at app startup - I keep this on-screen at all times, maximized.  The "page" buttons on each page invoke the following:
  Dim Form2 as frmPage2  (<- inserting the appropriate page/form# for the form to go to).
  Form2.Show()
  Me.Close()

Each the button on each form invokes the next desired page maximized - to "cover up" the main screen (Form1).  Navigating back to the "main" form (Form1) simply invokes Me.Close(), as Form1 always stays put.

I have then removed the Min/Max buttons and Control Box on all forms aside from Form1 to ensure the program isn't closed/exited from anywhere but Form1.  Since Form1 never closes, when it IS closed, the app shuts down.

No doubt, an inefficient way to go, and will probably be a nightmare to upkeep, but is the only way I actually understand how it all works.

As mentioned above, I'll probably run back to VB5 and recreate the app there <sigh>.

Thanks for all your input and help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now