Solved

Application won't shut down

Posted on 2006-07-08
4
314 Views
Last Modified: 2010-04-23
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
Comment
Question by:CThomp2005
[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
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 225 total points
ID: 17067076
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
 
LVL 12

Assisted Solution

by:Praveen Kumar
Praveen Kumar earned 225 total points
ID: 17067077
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
 
LVL 8

Assisted Solution

by:hiteshgupta1
hiteshgupta1 earned 50 total points
ID: 17079992
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
 
LVL 3

Author Comment

by:CThomp2005
ID: 17080584
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

634 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