locked
Method called by the "X" button RRS feed

  • Question

  • Hi,

    What is the method called when we click on the X button (Top and Right of the form) to exit an application?

    I have "cleanup" code in  Form1_FormClosing, but the application doesn't exit in the same state when I exit by the X button or by a menu calling Application.Exit.

    What is the "official" name of this X button?

    Thanks,

    Jean-François



    Monday, October 27, 2008 1:55 PM

Answers

  • Thats very interesting,  Application.Exit() does not close application!!!!, it is meant to close all forms(.net 2.0 or higher)

    Have you canceled ClosingEventArgs of objToolBox, that might be a probelm if yes.


    check for something like in objtoolbox form:

     Private Sub objToolBox_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
            e.Cancel = True
        End Sub

    Are you using .net 1.1 or higher?
    Me.close is only used to close the form you are calling this method from.

    Me.close closing entire application in your case since FormMain   is the owner of objToolBox, so while closing FormMain, objToolBox is also disposed without call formclosing(if exist).

    But Application.Exit checks formclosing event while closing the application and disposes.





    Arjun Paudel
    • Edited by Arjun Paudel Tuesday, October 28, 2008 5:05 AM
    • Marked as answer by jfgagnon34 Thursday, October 30, 2008 1:20 AM
    Tuesday, October 28, 2008 5:04 AM

All replies

  • Just a random guess but try your cleanup code in form1_formclosed.
    Monday, October 27, 2008 2:41 PM
  • What do you mean the application isn't exiting in the same state?  As in you want to do a different cleanup if the X is clicked?

    Anyways try this thread:
    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e6fa852c-857a-449f-b741-d9120f4f4f86
    Monday, October 27, 2008 5:49 PM
  • Thanks for the help!

    I was using Application.exit to end the program.  I replaced it by Me.Close, and everything works as it should.

    Jean-François
    Monday, October 27, 2008 8:34 PM
  • jfgagnon34 said:

    Thanks for the help!

    I was using Application.exit to end the program.  I replaced it by Me.Close, and everything works as it should.

    Jean-François

    Hi Jean-Francois

    Me.Close will close the FORM the code is within which is okay if you only have one FORM.

    You could also use END

    Application.Exit is better used where you have more than one FORM to close all the FORMs.


    Regards,

    John

    For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835
    Monday, October 27, 2008 9:27 PM
  • Thank you for your answer,

    In my current project, I use one main form and a second form as a toolbox.

    I have made a simpler project to understand how to close the program.

    - FormMain with two buttons, QuitButton and ShowToolButton.
    - FormTool, an empty form


    Public Class FormMain  
        Dim objToolBox As New FormTool  
     
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            objToolBox.Hide()  
        End Sub 
     
        Private Sub ShowToolButton_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles ShowToolButton.Click  
            objToolBox.Show()  
        End Sub 
     
        Private Sub QuitButton_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles QuitButton.Click  
            Application.Exit()  
        End Sub 
    End Class 

    If I click on QuitButton, nothing happens, but if I click on the X button, the program ends.

    If I replace Application.Exit by Me.Close, the Quitbutton will end the program.

    If you could shed some light, I would appreciate it!

    Thanks

    Jean-Francois
    Monday, October 27, 2008 11:53 PM
  • Hi again,

    I thought Application.Exit would also end the program ( it sounds odd to me ) but also check the

    ShutDown Mode

    Go to the PROJECT menu and select PROPERTIES at the bottom, in the initial APPLICATION tab on the left

    Choose between;

    When Startup Form Closes

    'or

    When Last Form Closes

    in the ShutDown Mode: ComboBox

    Regards,

    John

    For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835
    Tuesday, October 28, 2008 2:05 AM
  • Thats very interesting,  Application.Exit() does not close application!!!!, it is meant to close all forms(.net 2.0 or higher)

    Have you canceled ClosingEventArgs of objToolBox, that might be a probelm if yes.


    check for something like in objtoolbox form:

     Private Sub objToolBox_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
            e.Cancel = True
        End Sub

    Are you using .net 1.1 or higher?
    Me.close is only used to close the form you are calling this method from.

    Me.close closing entire application in your case since FormMain   is the owner of objToolBox, so while closing FormMain, objToolBox is also disposed without call formclosing(if exist).

    But Application.Exit checks formclosing event while closing the application and disposes.





    Arjun Paudel
    • Edited by Arjun Paudel Tuesday, October 28, 2008 5:05 AM
    • Marked as answer by jfgagnon34 Thursday, October 30, 2008 1:20 AM
    Tuesday, October 28, 2008 5:04 AM
  •  Hello John,

    Thanks for your comments.

    I have tested both options with the sample code I shown earlier (the two forms), and the results are the same.

    -When I call Application.Exit with the QuitButton, the application doesn't exit.

    -When I click on the X button, the application exits.

    -If I use Me.Close instead of Application.Exit, the QuitButton works.


    If I remove the second form, leaving only the Quitbutton , Application.exit works.  I suppose I don't use the right way to declare my second form, but since Me.Close works, I going to leave it that way.

    Thanks for your help!
    Tuesday, October 28, 2008 1:44 PM
  •  Hello Arjun,
     
    Thanks for the comments.


    You were right, I was using Form_Closing on the second form.

    I'm gonna use Me.Close in my application.

    Thanks for the explanation between Me.Close and Application.Exit.

    Jean-Francois
    • Edited by jfgagnon34 Tuesday, October 28, 2008 2:11 PM
    Tuesday, October 28, 2008 2:02 PM