Ok,
I have been toying around with this subject. I don't know how close I am
and what pitfalls I should look out for but let's see how close we are with
this:
Private WithEvents objDS As DelegateSample
Private Sub FrmTest_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
objDS = New DelegateSample
MsgBox(objDS.HelloWorld)
objDS.Close()
End Sub
Private Sub objDS_Closing(ByVal sender As Object, ByVal e As
DelegateSampleArgs) Handles objDS.Closing
e.Cancel = True
End Sub
Private Sub objDS_Closed(ByVal Sender As Object, ByVal e As
System.EventArgs) Handles objDS.Closed
MsgBox("Our object has closed")
End Sub
Private Sub objDS_WhoAreYou(ByVal sender As Object, ByVal e As
DelegateSampleWhoAreYouArgs) Handles objDS.WhoAreYou
e.WhoAmI = System.Security.Principal.WindowsIdentity.GetCurrent.Name
End Sub
Public Class DelegateSample
Delegate Sub CanClose(ByVal sender As Object, ByVal e As
DelegateSampleArgs)
Delegate Sub DoWhoAreYou(ByVal sender As Object, ByVal e As
DelegateSampleWhoAreYouArgs)
Event WhoAreYou As DoWhoAreYou
Event Closing As CanClose
Event Closed(ByVal Sender As Object, ByVal e As System.EventArgs)
Public Sub Close()
Dim e As DelegateSampleArgs
e = New DelegateSampleArgs
RaiseEvent Closing(Me, e)
Debug.WriteLine(e.Cancel.tostring)
If e.Cancel Then RaiseEvent Closed(Me, New System.EventArgs)
End Sub
Public Function HelloWorld()
Dim e As DelegateSampleWhoAreYouArgs
e = New DelegateSampleWhoAreYouArgs
e.WhoAmI = ""
RaiseEvent WhoAreYou(Me, e)
Return e.WhoAmI
End Function
End Class
Public Class DelegateSampleArgs
Inherits System.EventArgs
Private m_Cancel As Boolean
Public Sub New()
MyBase.New()
m_Cancel = False
End Sub
Public Property Cancel() As Boolean
Get
Return m_Cancel
End Get
Set(ByVal Value As Boolean)
m_Cancel = Value
End Set
End Property
End Class
Public Class DelegateSampleWhoAreYouArgs
Inherits System.EventArgs
Private m_WhoAmI As String
Public Sub New()
MyBase.New()
End Sub
Public Property WhoAmI() As String
Get
Return m_WhoAmI
End Get
Set(ByVal Value As String)
m_WhoAmI = Value
End Set
End Property
End Class