[RESOLVED] [2005] Experimenting with Namespaces and Classes I Don't Use: Question
So I thought I'd look around some just to learn more about Classes I never use.
Some of the following code doesn't work, can anyone educate me as to why not? I'm quite certain that I'm showing some level of ignorance here...:rolleyes:
Code:
Imports Microsoft.VisualBasic.ApplicationServices
Public Class Form1
Dim WithEvents wab As WindowsFormsApplicationBase = New WindowsFormsApplicationBase
#Region " Try to use the Shutdown Event "
Private Sub wab_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles wab.Shutdown
MessageBox.Show("Shut Down -- This Never Happens")
End Sub
#End Region
#Region " Try to use the Startup Event "
Private Sub wab_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles wab.Startup
MessageBox.Show("Starting Up -- This Never Happens")
End Sub
#End Region
#Region " Don't handle an exception and see if the UnhandledException Event can be used. "
'Force an error from within the Button1.Click Event Below
Private Sub wab_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles wab.UnhandledException
MessageBox.Show("UnhandledException This Doesn't Ever Show")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Try
'Cause Overflow
Dim x As Integer = 999999999
x *= 10
'Catch ex As Exception
'MessageBox.Show("Caught")
'End Try
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Show that wab actually points to an object
MessageBox.Show(wab.OpenForms.Count.ToString)
End Sub
End Class
How is the WindowsFormsApplicationBase class supposed to be used?
Re: [2005] Experimenting with Namespaces and Classes I Don't Use: Question
The My.Application object is an instance of that type and that's how it's intended to be used 99.9% of the time. Those events are accessed by clicking the View Application Events button on the Application tab of the project properties. Basically all new namespaces and types below the Microsoft.VisualBasic namespace have been created to support the features of the new My namespace.
Re: [2005] Experimenting with Namespaces and Classes I Don't Use: Question
Yes. I did notice that these properties that were showing were those I had seen in the My.Application object. In fact the stack trace kept coming up with the My.Application object even though I created a WindowsFormsApplicationBase object.
Nevertheless, the object, "wab" that I created should still work shouldn't it?
I'm probably making some basic mistake in my assumption there, but shouldn't the events still have been fired?
I'll try the My.Application Object next just to see.
Quote:
Originally Posted by jmcilhinney
The My.Application object is an instance of that type and that's how it's intended to be used 99.9% of the time. Those events are accessed by clicking the View Application Events button on the Application tab of the project properties. Basically all new namespaces and types below the Microsoft.VisualBasic namespace have been created to support the features of the new My namespace.
Re: [2005] Experimenting with Namespaces and Classes I Don't Use: Question
Ok, well just like you said this worked:
And here's the reason why the other events weren't working:
' 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.
Thanks again JM
Code:
Partial Friend Class MyApplication
Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
MessageBox.Show("Shut Down")
End Sub
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
MessageBox.Show("Start Up")
End Sub
End Class
Re: [2005] Experimenting with Namespaces and Classes I Don't Use: Question
Of course it didn't help me to find this in the object browser:
Public Event Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs)
Member of: Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
Summary:
Occurs when the application starts.
and this:
Public Event Shutdown(ByVal sender As Object, ByVal e As System.EventArgs)
Member of: Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
Summary:
Occurs when the application shuts down.
It could have been more accurate i.e. "Before" & "After" instead of "when"
Re: [RESOLVED] [2005] Experimenting with Namespaces and Classes I Don't Use: Questio
Actually, those descriptions are completely accurate as they are. The startup form is created after the application starts, so if the Startup event is raised "when the applications starts" then that is before the startup form is created. Likewise the last form is closed before the application shuts down, so if the Shutdown event is raised "when the application shuts down" then that is after the last form is closed. The problem is the way you were looking at things. The application is NOT the forms and the forms are NOT the application. This is why it's important to use the correct terms in the correct context.
Re: [RESOLVED] [2005] Experimenting with Namespaces and Classes I Don't Use: Question
You are correct. I do tend to look at a form as being the application.
Thanks for pointing that out.
It is a little disturbing that you can read my mind better than I can :eek2: