VB.NET一个秘密的类:简单但强大

Namespace My
    '右击解决方案的属性时,在“Application(应用)”中,单击“View  Application Events”便自动产生一个新的ApplicationEvents.vb。在此
    '里面共有五个事件。

    '这是特别的类,几乎被很多开发者忽略,但是功能最简单,却是却实用的类,
    ' The following events are available 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
        '检测计算是否接入网络
        Private Sub MyApplication_NetworkAvailabilityChanged(sender As Object,e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
            SetConnectionStatus(e.IsNetworkAvailable)
        End Sub

        '当应用程序各个窗体都关闭了。这个程序才运行,也是最后运行的程序
        Private Sub MyApplication_Shutdown(sender As Object,e As System.EventArgs) Handles Me.Shutdown
            MsgBox("程序已经关闭运行!")
        End Sub

        '当应用程序最开始启动时就运行了。早于任何窗体,在这个地方来初始化变量是最好的
        Private Sub MyApp_Startup(ByVal sender As Object,ByVal e As Microsoft.VisualBasic.applicationservices.StartupEventArgs) Handles Me.Startup
            ' When the application starts,set the connection status on the status strip
            MsgBox("程序将要开始启动")

        End Sub

        Public Sub SetConnectionStatus(ByVal connected As Boolean)
            'With My.Forms.MainForm.ConnectedStatusLabel
            '    If (connected) Then
            '        .Image = My.Resources.connected.ToBitmap
            '        .Text = My.Resources.ConnectedText
            '    Else
            '        .Image = My.Resources.disconnected.ToBitmap
            '        .Text = My.Resources.disconnectedText
            '    End If
            'End With
        End Sub

        Private Sub MyApplication_StartupNextInstance(sender As Object,e As Microsoft.VisualBasic.applicationservices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            MsgBox("程序已经启动一次了")
        End Sub

        '当程序在运行中出错,而未作处理时,它是来收集错误提示错误内容
        Private Sub MyApplication_UnhandledException(sender As Object,e As Microsoft.VisualBasic.applicationservices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            MsgBox("程序在运行中出现错误")
        End Sub
    End Class


End Namespace

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...