Application level events in WinForms
View the properties of your Winforms project, and on the "Application" page, you'll see the "View Application Events" button. Click this button and an "ApplicationEvents.vb" class is automatically added to your project, with the following code:
Namespace
My
' The following events are availble for MyApplication:
'
' Startup: Raised when the application starts, before the startup form is created.
' Shutdown: Raised after all application forms are closed. This event is not raised if the application terminates abnormally.
' UnhandledException: Raised if the application encounters an unhandled exception.
' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
Partial Friend Class MyApplication
End Class
End
Namespace
View the "Delcarations" drop down menu from the top right of the text editor, and you'll be able to add the following Application Level events to this class, automatically:
Private Sub MyApplication_NetworkAvailabilityChanged(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
End Sub
Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
End Sub
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
End Sub
Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
End Sub
Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
End Sub
The MSDN For Visual Studio documentation has these descriptions for the events:
| Event |
Description |
|
NetworkAvailabilityChanged |
Occurs when the network availability changes.
This event is available only for Windows Forms applications. |
|
Shutdown |
Occurs when the application shuts down.
This event is available only for Windows Forms applications. |
|
Startup |
Occurs when the application starts.
This event is available only for Windows Forms applications. |
|
StartupNextInstance |
Occurs when attempting to start a single-instance application and the application is already active.
This event is available only for Windows Forms applications. |
|
UnhandledException |
Occurs when the application encounters an unhandled exception.
This event is available only for Windows Forms applications. |
We finally get Application level events in our WinForms apps! We can easily know when our application is starting, shutting down, when the network status changes, and most importantly - we finally have a truly global event handler for unhandled exceptions! There is now a single, common location where we can put our "OMG!" error handling code for any unexpected errors that occur in our applications.