Determine if an event has been attached to yet

前端 未结 4 1131
悲哀的现实
悲哀的现实 2021-01-12 07:12

I have two objects - one that contains some code with will fire an event, and one that contains the handler for that event. I can\'t \"AddHandler\" in the Load of the first

4条回答
  •  隐瞒了意图╮
    2021-01-12 07:34

    VB.Net creates a special private member variable in the pattern of Event that you can then use to test against Nothing.

    Public Event MyClick As EventHandler
    
    Private Sub OnMyClick()
        If MyClickEvent IsNot Nothing Then
            RaiseEvent MyClick(Me, New EventArgs())
        Else
            ' No event handler has been set.
            MsgBox("There is no event handler. That makes me sad.")
        End If
    End Sub
    

    http://blogs.msdn.com/b/vbteam/archive/2009/09/25/testing-events-for-nothing-null-doug-rothaus.aspx

提交回复
热议问题