Category: event-handling

Aug 27 2009

How do you create a cancelable event in vb.net

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

Aug 13 2009

What is the proper way to setup events for inheritance

Hello,

Normally I setup events like this...

Public Delegate Sub MySpecialEventHandler(sender as object, e as mySpecialEventEventArgs)  '  ...I will not show the implementation of mySpecialEventArgs.
Public Event MySpecialEvent as MySpecialEventHandler  

Private Sub OnMySpecialEvent(newStatus as string)  
     Raise Event MySpecialEvent(Me,New mySpecialEventEventArgs(newStatus))  
End Sub

I have an interface...it looks like this...

Public Interface IWizardUserControl
    ReadOnly Property ShowNavigatePrevious() As Boolean
    ReadOnly Property ShowNavigateNext() As Boolean
    ReadOnly Property ShowNavigateFinish() As Boolean
    ReadOnly Property ShowNavigateCancel() As Boolean
    ReadOnly Property Description() As String
    ReadOnly Property StepCaption() As String
    ReadOnly Property PageImage() As System.Drawing.Image
    ReadOnly Property PageHelpText() As String
    Property IsValid() As Boolean
    Sub OnValidStatusChanged(ByVal validStatus As Boolean)
    Event ValidStatusChanged()
End Interface

I would like the event setup such as what you see above to be implemented in the above interface.

When you setup a delegate in the interface. No errors. But when you try to implement the delegate in the userControl Visual studio says "Delegates cannot implement interface methods" .

My goal is to have a controller class that would cast the derived class as the interface and then add a handler for the interfaces event.

Any help would be appreciated. Thanks in advance.

EDIT - I took John Saunders advice and drilled into the web.ui class. Here is what I did and it is working perfectly.

In the interface I added/changed the following...

Sub OnValidStatusChanged(ByVal validStatus As Boolean)  
Sub ValidStatusChangedHandler(ByVal sender As Object, ByVal e AsValidStatusChangedEventArgs)  
Event ValidStatusChanged As EventHandler

Most relevant point being that I ValidStatusChangedHandler is declared as a Sub not a delegate.

I implemented those in my derived class as follows...

Protected Event ValidStatusChanged As EventHandler Implements IWizardUserControl.ValidStatusChanged

Protected Sub ValidStatusChangedHandler(ByVal sender As Object, ByVal e As ValidStatusChangedEventArgs) Implements IWizardUserControl.ValidStatusChangedHandler
    OnValidStatusChanged(e.ValidStatus)
End Sub

Private Sub OnValidStatusChanged(ByVal status As Boolean) Implements IWizardUserControl.OnValidStatusChanged
    RaiseEvent ValidStatusChanged(Me, New ValidStatusChangedEventArgs(status))
End Sub

Finally in the controller class I am doing this...

Dim iwp As IWizardUserControl = DirectCast(wizardUserControl, IWizardUserControl)
AddHandler iwp.ValidStatusChanged, AddressOf iwp.ValidStatusChangedHandler

Works like a charm.

Seth