为 VB.NET 的类增加自定义事件

前端之家收集整理的这篇文章主要介绍了为 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
原文链接:https://www.f2er.com/vb/257547.html

猜你在找的VB相关文章