Solved

Application won't shut down

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

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!

Question has a verified solution.

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

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 was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

763 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