'VB.net 自定义事件使用方法

'VB.net 自定义事件使用方法
'1.先定义包含事件的类(如下)

Public Class TestEvent
    Public Event MyEvent(ByVal s As String)     '自定义事件
    
    Public Sub StartEvent(ByVal value As String) '触发事件函数
        RaiseEvent MyEvent(value)          '触发事件
    End Sub
End Class

'2.在使用事件的类中添加如下代码
   
      '声明事件对象
      Private WithEvents mEvent As TestEvent
      '实例化
      mEvent = New TestEvent
      '处理事件函数 注意:函数名必须为 变量名_事件名
       Private Sub mEvent_MyEvent(ByVal s As String) Handles mEvent.MyEvent
           MsgBox(s)
       End Sub
       '触发事件
      mEvent.StartEvent("11111")

3.
      '声明事件对象
      Private mEvent As TestEvent
     '实例化以及绑定处理函数
       mEvent = New TestEvent
         AddHandler (mEvent.MyEvent), AddressOf OnMyEvent   

      '处理事件函数
        Private Sub OnMyEvent(ByVal s As String) Handles mEvent.MyEvent
            MsgBox(s)
        End Sub     
          '触发事件
      mEvent.StartEvent("2222222")

vb.net 中自定义类中的事件

基本思路:
1,首先在要定义事件的类中声明事件,然后使用RaiseEvent 激发该事件.
Public Class Person

  Private name As String

   Public Event walked(ByVal distance As Integer)
   
  Public Sub onwalk(ByVal distance As Integer)
    RaiseEvent walked(distance)
  End Sub
End Class
2. 使用WithEvents 声明该类的对象.
  Friend WithEvents myperson As Person
3,编写事件处理代码.
  Private Sub myperson_walked(ByVal distance As Integer) - 
   Handles myperson.walked
   TextBox1.Text = "walked" & distance
   End Sub
4,调用事件.
  Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)  Handles Button1.Click
  myperson.onwalk(20)
  End Sub

Vb.Net自定义事件的应用及应注意的地方
主窗体: 
Public Event msg(ByVal i As Int16)   '''首先定义一个事件

Dim i As Int16 '''定义一个变量
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        i += 1
        RaiseEvent msg(i) '''激发事件
    End Sub
  

    Dim frm As Addition = Nothing '''从窗体
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If frm Is Nothing Then
            frm = New Addition
            frm.main = Me '''此处是应该注意的地方,一定要把从窗体定义的主窗体设置成此窗体,否则不会引发事件!
        End If

      '''上面定义从窗体的方法,可以避免多次实例化窗体

        frm.Show()
    End Sub

从窗体:

Public WithEvents main As main '''定义主窗体事件并执行

    Private Sub main_msg(ByVal i As Short) Handles main.msg
        Label1.Text = i
    End Sub

1,以上方法是主窗体定义事件,然后触发,在从窗体执行的情况,需要注意的是frm.main=me

此句的意思是从窗体的触发事件是主窗体定义的

2,但是在现实应用中,通常都是从窗体内容发生变化,需要及时更新主窗体,此时代码有小小的变动,如下,

需要仔细看啊!

主窗体:

   Public WithEvents add As Addition
    Dim frm As Addition = Nothing
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If frm Is Nothing Then
            frm = New Addition
            Me.add = frm    '''注意此处的区别
        End If
        frm.Show()
    End Sub

    Private Sub add_msg(ByVal i As Short) Handles add.msg
        Label1.Text = i
    End Sub

从窗体:

       Public Event msg(ByVal i As Int16)
    Dim i As Int16

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        i += 1
        RaiseEvent msg(i)
    End Sub

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页