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

前端之家收集整理的这篇文章主要介绍了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

原文链接:https://www.f2er.com/vb/260851.html

猜你在找的VB相关文章