Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exit App by Exit btn or X

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,569
US

I would like to find the way to exit my app by Exit command button or by an X in the upper-right corner of the Forms.

I did try to call this:
Code:
    Public Function ExitApplication() As Boolean
        If MessageBox.Show("Are you sure you want to exit?", "Exit?", _
            MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
            MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
            Return True
        Else
            Return False
        End If
    End Function
from Exit command button and X, and have this in FormClosing
Code:
        If e.CloseReason = CloseReason.UserClosing Or _
        e.CloseReason = CloseReason.ApplicationExitCall Then
            e.Cancel = Not ExitApplication()
        End If
It works fine when called from first Form's Exit and X, but the application does not end when I call it from other Forms (Form2). In IDE I have to end it by "Stop Debugging" icon.

I would like to end my application in one place - I need to do some cleaning, unlocking DB and other stuff.

VB.NET 2008

In VB 6 this did the trick, but VB.NET does not like it:
Code:
    Dim frmMyForm As Form

    For Each frmMyForm In Forms
        Unload frmMyForm
        Set frmMyForm = Nothing
    Next frmMyForm

Have fun.

---- Andy
 
So you have an application, with multiple forms, that can be exited from ANY of the forms using the X or an Exit button??? That doesn't seem to be logical in my opinion.

You probably have a main screen of some sort and child forms opened from this form. When you click the X on a child form, should you not close the child form???

The code above should work fine for the main screen, and you indicate it does. The other forms would need to just close themselves down and return back to the main screen.

You also might want to look at the properties of the project...there is a setting (ShutdownMode) that lets you choose the application close method. You can choose from: When startup form closes or When last form closes.

If I am way off base, please describe your scenario a bit more so I can understand what it is you are trying to accomplish....

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
First, make sure your programs Shutdown mode is "When last form closes". Second, get rid of the function and the form close should look like this:

Code:
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    If msgbox("Are you sure you want to exit?", MsgBoxStyle.YesNo, "Exit") = MsgBoxResult.No then
        e.Cancel = True
    End If
End Sub
Anything else and you are just over complicating the whole thing.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 

Thank you Robert for looking at my problem.

The scenario is – I need to re-write a system written in VB 6 to VB.NET 2008. It is not a parent – child, just a whole lot of ‘independent’ Forms that are open, close, hidden, shown, etc. In VB 6 I disabled the X on the Forms so users can exit only by clicking Exit button(s), but now I thought it would be nice to give them the ability to use X to bail out from the application.

Yes, I would like to exit from ANY Form.

No, I don’t have a Main form with child forms (not my call :-( )

I did look at the properties of the Project – I can not use “When startup form closes” because app would end as soon as I move to second Form disposing the first, which I don’t really need any more. If I do that anyway (and Hide the first Form), the second Form, when closing, goes to ExitApplication twice and does not end application anyway.


Have fun.

---- Andy
 
My Parent-Child terminology may not have been the best. I did not mean a true Parent-Child relationship....only that there is one form that acts as the root for the application. All the others are opened from this form or similar.

In a typical application, the root form is the only place to EXIT the APPLICATION. All other forms just close themselves and pass focus back to the root form or another of the sibling forms, depending on where it was called from.

I cannot begin to understand how your application can operate outside of this scenario. I feel there has to be a root form of some sort that acts "control" for the rest of the application. The only scenario I can see this in is when you have the "root" form open a bunch of others at Load. These form basically fill up the screen and can be moved around independently. And if you close one of them you close them all. Real world example might be a group of forms that display webcam feeds...they all are display or none. Not that i would ever want to do that but I guess you could. But this scenario - you could probably make much better use with an MDI setup.

Please give some example/description of what this application is and how it works so I can understand your scenario better. Thanks.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
did look at the properties of the Project – I can not use "When startup form closes" because app would end as soon as I move to second Form disposing the first, which I don't really need any more. If I do that anyway (and Hide the first Form), the second Form, when closing, goes to ExitApplication twice and does not end application anyway.
So you are saying you already have it set to "When last form closes".

Did you try uncomplicating the close? While everything you did looks right I would still try it. The only other thing that comes to mind is if you are threading. If the thread is still running it would prevent the program from closing even if all forms are closed. Make sure you have closed all threads/delegates.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 

OK, this is my 'uncomplicating' code, which works the same way as before:
Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        Application.Exit()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click

        Form2.Show()
        Me.Dispose()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.FormClosingEventArgs) _
        Handles Me.FormClosing

        If MsgBox("Are you sure you want to exit?", _
                  MsgBoxStyle.YesNo, "Exit") = MsgBoxResult.No Then
            e.Cancel = True
        End If
    End Sub
End Class
Code:
Public Class Form2

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        Application.Exit()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
        Form1.Show()
        Me.Dispose()
    End Sub

    Private Sub Form2_FormClosing(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.FormClosingEventArgs) _
        Handles Me.FormClosing
        If MsgBox("Are you sure you want to exit?", _
                  MsgBoxStyle.YesNo, "Exit") = MsgBoxResult.No Then
            e.Cancel = True
        End If
    End Sub
End Class
On both Forms, Button1 is Exit, Button2 calls the other Form. Works fine when only Form1 is exited, but when Form2 is called, Exit button works OK, but the X on the Form2 does not end the app.

:-(

Have fun.

---- Andy
 
When checking the close on a form you don't want to do an Application.Exit on any form. Instead switch them all to Me.Close(). This way you are making sure you are only ending the application at one point. You never have to question where you may have stopped the program. None of that is the problem though.

I just spent some time since it has been a while since I used that way to close a program. When set to "When last form closes" you still have to tell it when you are done. So switch the form close to:

Code:
Private Sub Form2_FormClosing(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.FormClosingEventArgs) _
        Handles Me.FormClosing
        If MsgBox("Are you sure you want to exit?", _
                  MsgBoxStyle.YesNo, "Exit") = MsgBoxResult.No Then
            e.Cancel = True
        Else
            Me.Dispose()
            End
        End If
    End Sub

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 

Thanks,

Not crazy about End. Was told many times to never use End in my code.

Have fun.

---- Andy
 
You really shouldn't, of the ways you can stop the program with the setting "When last form closes" a Dispose() with an End seems the best. That way you are sure the program stops. If you do just an application.exit you can run the risk of a hang-up. You can still do it that way and see, just replace the dispose and end with application.exit.

The only other way would be to set it to when start form closes and hide the main form like you mentioned. Really there should be a way to setup everything so you are only getting the message once.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top