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

前端之家收集整理的这篇文章主要介绍了VB.NET一个秘密的类:简单但强大前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. Namespace My
  2. '右击解决方案的属性时,在“Application(应用)”中,单击“View Application Events”便自动产生一个新的ApplicationEvents.vb。在此
  3. '里面共有五个事件。
  4.  
  5. '这是特别的类,几乎被很多开发者忽略,但是功能最简单,却是却实用的类,
  6. ' The following events are available for MyApplication:
  7. '
  8. ' Startup: Raised when the application starts,before the startup form is created.
  9. ' Shutdown: Raised after all application forms are closed. This event is not raised if the application terminates abnormally.
  10. ' UnhandledException: Raised if the application encounters an unhandled exception.
  11. ' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
  12. ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
  13. Partial Friend Class MyApplication
  14. '检测计算是否接入网络
  15. Private Sub MyApplication_NetworkAvailabilityChanged(sender As Object,e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
  16. SetConnectionStatus(e.IsNetworkAvailable)
  17. End Sub
  18.  
  19. '当应用程序各个窗体都关闭了。这个程序才运行,也是最后运行的程序
  20. Private Sub MyApplication_Shutdown(sender As Object,e As System.EventArgs) Handles Me.Shutdown
  21. MsgBox("程序已经关闭运行!")
  22. End Sub
  23.  
  24. '当应用程序最开始启动时就运行了。早于任何窗体,在这个地方来初始化变量是最好的
  25. Private Sub MyApp_Startup(ByVal sender As Object,ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
  26. ' When the application starts,set the connection status on the status strip
  27. MsgBox("程序将要开始启动")
  28.  
  29. End Sub
  30.  
  31. Public Sub SetConnectionStatus(ByVal connected As Boolean)
  32. 'With My.Forms.MainForm.ConnectedStatusLabel
  33. ' If (connected) Then
  34. ' .Image = My.Resources.connected.ToBitmap
  35. ' .Text = My.Resources.ConnectedText
  36. ' Else
  37. ' .Image = My.Resources.disconnected.ToBitmap
  38. ' .Text = My.Resources.DisconnectedText
  39. ' End If
  40. 'End With
  41. End Sub
  42.  
  43. Private Sub MyApplication_StartupNextInstance(sender As Object,e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
  44. MsgBox("程序已经启动一次了")
  45. End Sub
  46.  
  47. '当程序在运行中出错,而未作处理时,它是来收集错误提示错误内容
  48. Private Sub MyApplication_UnhandledException(sender As Object,e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
  49. MsgBox("程序在运行中出现错误")
  50. End Sub
  51. End Class
  52.  
  53.  
  54. End Namespace
  55.  

猜你在找的VB相关文章