Results 1 to 30 of 30

Thread: formclosing, formclosed and applicaton events not firing when app close

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2026
    Posts
    12

    formclosing, formclosed and applicaton events not firing when app close

    I'm going crazy. When I close my winforms app, by closing the start form, formclosing, formclosed or applicationevents are not executed.

    Something is messing with my code so that it is skipped.

    I have telerik version 2016 installed.

    I have VS2022 or VS2026, .NET framework 4.8 (it is about to be an older app)

    Anyone have an idea what could be wrong, or if there is another way to tell that you are about to exit the windows app?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    Re: formclosing, formclosed and applicaton events not firing when app close

    Are you closing the form programmatically or is the user simply clicking the close icon?

    If it's the former, could you show the code that you're using?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2026
    Posts
    12

    Re: formclosing, formclosed and applicaton events not firing when app close

    Im just click the close icon in the upper right corner of the form

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    Re: formclosing, formclosed and applicaton events not firing when app close

    Hmm, ok.

    What are you doing to check if they're executing or not? For example, is it that you're setting up a breakpoint in the events and it's simply not breaking or are you doing something else?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2026
    Posts
    12

    Re: formclosing, formclosed and applicaton events not firing when app close

    Stop command, msgbox, some kind of code, nothing work.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    Re: formclosing, formclosed and applicaton events not firing when app close

    At this point, I'm inclined to believe that the event is not attached properly. This typically happens if you copy/paste forms, but it is a bit unusual to see happen to the application events.

    Could you copy/paste the method signature of the various points that should be firing, but aren't?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,510

    Re: formclosing, formclosed and applicaton events not firing when app close

    Quote Originally Posted by dday9 View Post
    At this point, I'm inclined to believe that the event is not attached properly. This typically happens if you copy/paste forms, but it is a bit unusual to see happen to the application events.

    Could you copy/paste the method signature of the various points that should be firing, but aren't?
    Yeah, it does sound like the Handles clause could be missing from the event. Actually seeing the event code would make things a lot clearer.

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2026
    Posts
    12

    Re: formclosing, formclosed and applicaton events not firing when app close

    Do you mean application events? When i opened it, it was actually empty, so i copy the events in. The app start event is actually working, but not the rest.

  9. #9
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,510

    Re: formclosing, formclosed and applicaton events not firing when app close

    Post the form closing event code.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: formclosing, formclosed and applicaton events not firing when app close

    FormClosing and FormClosed are events you can handle in your Form, as Form1_Click is an event you can handle in your Form.

    See this… https://learn.microsoft.com/en-us/do...-windows-forms

    These are part of your Form1 code, NOT part of application events…
    Last edited by .paul.; May 1st, 2026 at 04:28 AM.

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2026
    Posts
    12

    Re: formclosing, formclosed and applicaton events not firing when app close

    I know,.and as i wrote, its doesnt work.

  12. #12

    Thread Starter
    New Member
    Join Date
    Apr 2026
    Posts
    12

    Re: formclosing, formclosed and applicaton events not firing when app close

    Quote Originally Posted by wes4dbt View Post
    Post the form closing event code.
    Code:
      Private Sub MDImain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
         
         MsgBox ("Stop") DONT WORK
    
         'Stop DONT WORK
      
    
          Select Case e.CloseReason
              Case CloseReason.ApplicationExitCall
                  ' This is the result of Application.Exit()
                  e.Cancel = False
              Case CloseReason.UserClosing
                  ' This is the result of clicking the red X
                  Select Case MessageBox.Show("Are you sure you wish to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                      Case DialogResult.Yes
                          e.Cancel = False
                      Case DialogResult.No
                          e.Cancel = True
                  End Select
              Case Else
                  e.Cancel = False
          End Select
          If Not e.Cancel Then
    
          End If
    ALSO DONT WORK

    BY THE WAY, HERE IS MY APPLICATONEVENTS KODE:

    Code:
    Imports Microsoft.VisualBasic.ApplicationServices
    
    Namespace My
    
        'AddHandler(SystemEvents.SessionEnding, AddressOf ShutDown.OnShuttingDown)
        'AddHandler(SystemEvents.SessionEnded, AddressOf ShutDown.OnShutDown)
    
        ' Denne klasse håndterer globale applikationshændelser
        Partial Friend Class MyApplication
            Public Event ApplicationExit(sender As Object, e As EventArgs)
    
            ' Kører når applikationen starter
            
            Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
                'MsgBox("Programmet starter op.")
                'My.Application.Log.WriteEntry("Application started at " & My.Computer.Clock.GmtTime.ToString)
            End Sub
    
            ' Kører når applikationen er ved at lukke
            'Private Sub MyApplication_Shutdown(sender As Object, e As EventArgs) Handles Me.Shutdown
            '    'Process.Start("c:\test 01\Installationsvejledning.pdf") ' Eksempel på at starte en proces ved nedlukning
            '    'MsgBox("Programmet lukker ned.")
            'End Sub
    
            Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
                MsgBox("Pro")
            End Sub
    
            ' Kører når en ubehandlet fejl opstår
            Private Sub MyApplication_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
    
                'Debug.WriteLine("Der opstod en ubehandlet fejl: " & e.Exception.Message)
    
                ' Hvis du vil forhindre at programmet lukker:
                ' e.ExitApplication = False
            End Sub
    
            ' Kører når applikationen afsluttes (ApplicationExit)
            Private Sub MyApplication_ApplicationExit(sender As Object, e As EventArgs) Handles Me.ApplicationExit
                'Stop
                'Process.Start("c:\test 01\Installationsvejledning.pdf")
                MsgBox("ApplicationExit: Hele applikationen lukker ned.")
            End Sub
    
        End Class
    
    End Namespace
    In application event, 'MyApplication_Startup' actually works, not the rest.

    End Sub
    Last edited by dday9; May 1st, 2026 at 09:13 AM. Reason: Added code tags

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: formclosing, formclosed and applicaton events not firing when app close

    This might help...

    Quote Originally Posted by learn.microsoft
    If the form is a multiple-document interface (MDI) parent form, the FormClosing events of all MDI child forms are raised before the MDI parent form's FormClosing event is raised. Likewise, the FormClosed events of all MDI child forms are raised before the FormClosed event of the MDI parent form is raised. Canceling the FormClosing event of an MDI child form does not prevent the FormClosing event of the MDI parent form from being raised. However, canceling the event will set to true the Cancel property of the FormClosingEventArgs class that is passed as a parameter to the parent form. To force all MDI parent and child forms to close, set the Cancel property to false in the MDI parent form.

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: formclosing, formclosed and applicaton events not firing when app close

    @PeerDK

    Did that help? Is your issue resolved now? Don’t forget to mark your thread RESOLVED

  15. #15

    Thread Starter
    New Member
    Join Date
    Apr 2026
    Posts
    12

    Re: formclosing, formclosed and applicaton events not firing when app close

    I still can't get it to work. Not even from mdichilds, the formclosing and formclosed events aren't caught either.

    Does anyone have any good ideas? There must be something in my code that's making it not work.

  16. #16
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    Re: formclosing, formclosed and applicaton events not firing when app close

    Something is happening, but at this point you need to go back to basic debugging techniques.

    What I would do is bring your code down to the most minimal possible solution, e.g.
    Code:
    Private Sub MDImain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
    End Sub
    Then, slowly add code back, e.g.
    Code:
    Private Sub MDImain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        MessageBox.Show("MDImain_FormClosing")
        e.Cancel = True
    End Sub
    Then:
    Code:
    Private Sub MDImain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        MessageBox.Show("MDImain_FormClosing")
    
        e.Cancel = (e.CloseReason <> CloseReason.ApplicationExitCall)
    End Sub
    Then:
    Code:
    Private Sub MDImain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        MessageBox.Show("MDImain_FormClosing")
    
        Select Case e.CloseReason
            Case CloseReason.ApplicationExitCall
                e.Cancel = False
            Case CloseReason.UserClosing
                e.Cancel = (MessageBox.Show("Are you sure you wish to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> DialogResult.Yes)
        End Select
    End Sub
    ... and so on, until your code looks like it does in its current state.

    If at any point the code stops behaving like you expect it to, then you've found where your bug is.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  17. #17

    Thread Starter
    New Member
    Join Date
    Apr 2026
    Posts
    12

    Re: formclosing, formclosed and applicaton events not firing when app close

    I have already tried that. The sub is never activated.

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: formclosing, formclosed and applicaton events not firing when app close

    Try this...

    MDI_Example.zip

  19. #19
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    Re: formclosing, formclosed and applicaton events not firing when app close

    Quote Originally Posted by PeerDk View Post
    I have already tried that. The sub is never activated.
    I'm going to rephrase what I was originally going to respond with.

    What are you expecting to happen? I think there may be some confusion because, for example, one would never see this message box appear because the application is shutting down:
    Code:
    Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
        MsgBox("Pro")
    End Sub
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  20. #20

    Thread Starter
    New Member
    Join Date
    Apr 2026
    Posts
    12

    Re: formclosing, formclosed and applicaton events not firing when app close

    I got tired of not being able to find the error, so I made a button with the code Application.exit. Then form.closing and form.closed suddenly work.

    I simply don't understand why form.closing doesn't work when I press the red cross in the form, so the program closes.

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: formclosing, formclosed and applicaton events not firing when app close

    What i posted in post #18... Clicking X's works for me

  22. #22

    Thread Starter
    New Member
    Join Date
    Apr 2026
    Posts
    12

    Re: formclosing, formclosed and applicaton events not firing when app close

    Quote Originally Posted by .paul. View Post
    What i posted in post #18... Clicking X's works for me
    But it dont work when i Click x, thats the whole problem, as i explained in my first post. Form.closing does NOT work when closing the program normally, but apparently does when I use the code application.exit in a button I make.

  23. #23
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: formclosing, formclosed and applicaton events not firing when app close

    Quote Originally Posted by PeerDk View Post
    I got tired of not being able to find the error, so I made a button with the code Application.exit. Then form.closing and form.closed suddenly work.

    I simply don't understand why form.closing doesn't work when I press the red cross in the form, so the program closes.
    What red cross are you pressing?

    Are any exceptions being thrown?

  24. #24

    Thread Starter
    New Member
    Join Date
    Apr 2026
    Posts
    12

    Re: formclosing, formclosed and applicaton events not firing when app close

    Quote Originally Posted by PlausiblyDamp View Post
    What red cross are you pressing?

    Are any exceptions being thrown?
    Upper right corner, close form. Nothing at all happen in form.closing and form.closed also nothing in application events.

  25. #25
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: formclosing, formclosed and applicaton events not firing when app close

    Quote Originally Posted by PeerDk View Post
    Upper right corner, close form. Nothing at all happen in form.closing and form.closed also nothing in application events.
    When you have an MDI Form, clicking the X in a child Form doesn't raise the parent .Closing and .Closed events. Each Form has its own events...

  26. #26
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: formclosing, formclosed and applicaton events not firing when app close

    The order of events raised due to clicking the X are:

    (Clicking the X) In a child Form (also a regular Form), first the Form’s FormClosing event, then assuming you didn’t set Cancel, the Form’s FormClosed event.

    (Clicking the X) In an MDIForm, first the MDIForm’s FormClosing event, then assuming not cancelled, the FormClosing followed by FormClosed events of each child Form, finishing with the MDIForm’s FormClosed event.

  27. #27

    Thread Starter
    New Member
    Join Date
    Apr 2026
    Posts
    12

    Re: formclosing, formclosed and applicaton events not firing when app close

    Quote Originally Posted by .paul. View Post
    The order of events raised due to clicking the X are:

    (Clicking the X) In a child Form (also a regular Form), first the Form’s FormClosing event, then assuming you didn’t set Cancel, the Form’s FormClosed event.

    (Clicking the X) In an MDIForm, first the MDIForm’s FormClosing event, then assuming not cancelled, the FormClosing followed by FormClosed events of each child Form, finishing with the MDIForm’s FormClosed event.
    I know, but nothing happens.

  28. #28
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: formclosing, formclosed and applicaton events not firing when app close

    Well there are two alternatives in that case...

    Option 1. Create a new application, adding your previous app code to the new app, incrementally block by block, running and testing after each new addition. Find the code that breaks it, and we can probably help. (with your code in the My Namespace, AddHandler outside of a Class wouldn't work, and i've never seen much code used in Class MyApplication - I wouldn't recommend that.)

    Option 2. This option depends how huge your app is, and how private it is. I could have a look at it if you upload it somewhere.

  29. #29
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,510

    Re: formclosing, formclosed and applicaton events not firing when app close

    There is also the possibility that the forms designer code is corrupted.

    Option 1 above would solve that but if it's a large app you might try just creating a new form.

  30. #30
    New Member
    Join Date
    Dec 2022
    Posts
    3

    Re: formclosing, formclosed and applicaton events not firing when app close

    problem maybe your case statements not firing or the Closing sub is overridden by another one

    use this Protected Overrides Sub OnClosing(ByVal e As CancelEventArgs)

    in onClosing sub
    add only -------------------- e.Cancel = true

    if stop closing, work on the case statements

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width