zoukankan      html  css  js  c++  java
  • VB.Net 中 WithEvents、AddHandler

     
    Public Class Form1

        
    Dim WithEvents cls1 As New ClassTest()  'WithEvents方式
        Dim cls2 As New ClassTest()             'AddHandler方式

        
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            cls1.Test()                         
    'WithEvents方式

            
    AddHandler cls2.GetValue, AddressOf cls2GetValue    'AddHandler方式
            cls2.Test()

            
    Me.Close()
        
    End Sub


        
    Private Sub cls1_GetVale(ByVal str As StringHandles cls1.GetValue     'WithEvents方式
            MsgBox("cls1  " & str)
        
    End Sub


        
    Private Sub cls2GetValue(ByVal str As String)    'AddHandler方式
            MsgBox("cls2  " & str)
        
    End Sub



    End Class


    Public Class ClassTest

        
    Public Event GetValue(ByVal str As String)

        
    Public Sub Test()
            
    RaiseEvent GetValue("事件例子")
        
    End Sub


    End Class


    Result:
    cls1  事件例子
    cls2  事件例子
     
  • 相关阅读:
    3372 选学霸
    3556 科技庄园
    1025 选菜
    UVA 437 The Tower of Babylon巴比伦塔
    3641 上帝选人
    tyvj P1175 机器人
    1692 子集和的目标值
    1689 建造高塔
    NOI2002 贪吃的九头龙
    NYOJ110 剑客决斗
  • 原文地址:https://www.cnblogs.com/todd/p/1226516.html
Copyright © 2011-2022 走看看