Solved

Application won't shut down

Posted on 2006-07-08
4
304 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
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:Mamtha1982
Mamtha1982 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

22 Experts available now in Live!

Get 1:1 Help Now