How to expose and raise custom events for a vb.net winforms user control

18,758

Solution 1

To use custom events for bubbling the events of another control, you can do like this:

Public Custom Event AddRemoveAttendees As EventHandler
    AddHandler(ByVal value As EventHandler)
        AddHandler _theButton.Click, value
    End AddHandler

    RemoveHandler(ByVal value As EventHandler)
        RemoveHandler _theButton.Click, value
    End RemoveHandler

    RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
        ' no need to do anything in here since you will actually '
        ' not raise this event; it only acts as a "placeholder" for the '
        ' buttons click event '
    End RaiseEvent
End Event

In AddHandler and RemoveHandler you simply propagate the call to attach or remove the given event handler to/from the control's Click event.

To expand a bit on the use of custom events, here is another sample implementation of a custom event:

Dim _handlers As New List(Of EventHandler)
Public Custom Event AddRemoveAttendees As EventHandler

    AddHandler(ByVal value As EventHandler)
        _handlers.Add(value)
    End AddHandler

    RemoveHandler(ByVal value As EventHandler)
        If _handlers.Contains(value) Then
            _handlers.Remove(value)
        End If
    End RemoveHandler

    RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
        For Each handler As EventHandler In _handlers
            Try
                handler.Invoke(sender, e)
            Catch ex As Exception
                Debug.WriteLine("Exception while invoking event handler: " & ex.ToString())
            End Try
        Next
    End RaiseEvent
End Event

Now, as it looks above, it does little else than a regular event declaration:

Public Event AddRemoveAttendees As EventHandler

It provides a similar mechanism allowing for event handlers to be attached and removed, and for the event to be raised. What the Custom event adds is an extra level of control; you get to write some code around the adding, removing and raising of the event, in which you can enforce rules, and tweak what will happen a little bit. For instance, you may want to limit the number of event handlers that are attached to your event. To achieve that you can alter the AddHandler section from the sample above:

    AddHandler(ByVal value As EventHandler)
        If _handlers.Count < 8 Then
            _handlers.Add(value)
        End If
    End AddHandler

If you don't need that kind of detailed control, I see no need to declare custom events.

Solution 2

If you want the same thing as in the other post you mentionned, here's the VB.NET equivalent :

Public Custom Event AddRemoveAttendees As EventHandler

    AddHandler(ByVal value As EventHandler)
        AddHandler SaveButton.Click, value
    End AddHandler

    RemoveHandler(ByVal value As EventHandler)
        RemoveHandler SaveButton.Click, value
    End RemoveHandler

End Event

But I don't think that's a good idea, because the sender parameter of the event will be the Button, not your UserControl...

Instead, you could subscribe to the Button.Click event and raise you own event (with no explicit accessors)

Share:
18,758
Seth Spearman
Author by

Seth Spearman

Self-taught. Geek.

Updated on June 08, 2022

Comments

  • Seth Spearman
    Seth Spearman almost 2 years

    Please read THIS post. I have the same problem as described in this post but I am trying to do in in VB.net rather than c#.

    I am pretty sure to do this I have to use a custom event. (I used a code conversion site to get to learn about custom events.) So in the IDE when I type the following:

    Public Custom Event AddRemoveAttendees As EventHandler

    It expands to the following code snippet.

    Public Custom Event AddRemoveAttendees As EventHandler
        AddHandler(ByVal value As EventHandler)
    
        End AddHandler
    
        RemoveHandler(ByVal value As EventHandler)
    
        End RemoveHandler
    
        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
    
        End RaiseEvent
    End Event
    

    But I can't figure out what to do with it. Until today I had never heard of custom events.

    The bottom line of what I want is to have the click event of a button bubble up to the container of the user control. I know that I could wrap my own event but I would at least like to understand custom events before I go farther down that road.

    Seth