views:

187

answers:

1

IN VB.NET (not c#)...

I want to create an event than can be canceled by the listener. Just like you can cancel the closing event of a winforms form in which case the form won't close.

I have already implemented a derived class from EventArgs that has a settable Cancel property as follows:

Public Class AnnounceNavigateEventArgs
    Inherits EventArgs

    Private _cancel As Boolean = False

    ''' <summary>
    ''' Initializes a new instance of the AnnounceNavigateEventArgs class.
    ''' </summary>
    Public Sub New(ByRef cancel As Boolean)
        _cancel = cancel
    End Sub
    Public Property Cancel() As Boolean
        Get
            Return _cancel
        End Get
        Set(ByVal value As Boolean)
            _cancel = value
        End Set
    End Property

End Class

Notice that I am passing the cancel argument byRef to the constructor.

The listener I have setup is setting the property to Cancel=True. I thought ByRef meant that both _cancel and cancel would point to the same location on the stack and that setting _cancel=true would therefore make the cancel = true. But this is not the behavior I am getting. _cancel becomes true in the setter but I guess the argument to the constructor remains false.

What is the proper way to do this in vb.net?

Seth

+2  A: 

You can re-use the System.ComponentModel.CancelEventArgs class in the .NET framework.

Public Event Announcing As EventHandler(Of AnnounceNavigateEventArgs)

Protected Sub OnAnnounce()
    Dim e As New AnnounceNavigateEventArgs

    RaiseEvent Announcing(Me, e)

    If Not e.Cancel Then
        ' announce
    End If
End Sub

Public Class AnnounceNavigateEventArgs
    Inherits System.ComponentModel.CancelEventArgs
End Class
Fnop
Thanks...that did it.Seth
Seth Spearman