基类:
Public Class userFun Private Shared _PnlStatus As String ‘必须设为共享字段,如果不设为Shared,将不能传递字符串内容 Public Delegate Sub EventHandler() Public Shared Event PnlStatusChanged As EventHandler ’共享事件,如果不设为Shared,将不能触发主窗体中的事件 Public Property PnlStatus As String ‘类属性 Get Return _PnlStatus End Get Set(value As String) _PnlStatus = value RaiseEvent PnlStatusChanged() ’触发事件 End Set End Property End Class
用户自定义控件ucOrder中设置状态信息:
Public Class ucOrder Private objuserFun As New userFun objuserFun.PnlStatus = "新建订单!" End Class
主窗体中显示状态信息:
Public Class frmAdmin Private WithEvents objuserFun As New userFun ’添加WithEvents,显式调用事件 Private Sub objuserFun_PnlStatusChanged() Handles objuserFun.PnlStatusChanged PnlStatus.Text = objuserFun.PnlStatus ’主窗体显示状态信息 End Sub End Class
总结:VB中的共享事件感觉跟C#中的静态事件差不多。