Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 439
Seth Spearman Avatar asked Jul 03 '09 20:07

Seth Spearman


2 Answers

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.

like image 166
Fredrik Mörk Avatar answered Jan 02 '23 21:01

Fredrik Mörk


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)

like image 33
Thomas Levesque Avatar answered Jan 02 '23 21:01

Thomas Levesque