如何为vb.net winforms用户控件公开和引发自定义事件

时间:2022-01-22 03:32:57

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#.

请阅读这篇文章。我有同样的问题,如本文所述,但我试图在VB.net而不是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:

我很确定这样做我必须使用自定义事件。 (我使用代码转换站点来了解自定义事件。)因此在IDE中键入以下内容时:

Public Custom Event AddRemoveAttendees As EventHandler

公共自定义事件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.

我想要的底线是让按钮的click事件冒泡到用户控件的容器。我知道我可以包装自己的活动但我至少想在我走得更远之前了解自定义事件。

Seth

2 个解决方案

#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.

在AddHandler和RemoveHandler中,您只需传播调用以将给定的事件处理程序附加到控件的Click事件或从控件的Click事件中删除。

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:

它提供了一种类似的机制,允许附加和删除事件处理程序,以及引发事件。 Custom事件添加的内容是额外的控制级别;你可以围绕添加,删除和引发事件编写一些代码,在这些代码中你可以强制执行规则,并调整会发生什么。例如,您可能希望限制附加到事件的事件处理程序的数量。要实现这一点,您可以更改上面示例中的AddHandler部分:

    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.

如果您不需要这种详细控件,我认为无需声明自定义事件。

#2


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

如果你想要提到你提到的另一篇文章中的相同内容,这里是VB.NET的等价物:

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...

但我认为这不是一个好主意,因为事件的sender参数将是Button,而不是UserControl ......

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

相反,您可以订阅Button.Click事件并引发您自己的事件(没有显式访问者)

#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.

在AddHandler和RemoveHandler中,您只需传播调用以将给定的事件处理程序附加到控件的Click事件或从控件的Click事件中删除。

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:

它提供了一种类似的机制,允许附加和删除事件处理程序,以及引发事件。 Custom事件添加的内容是额外的控制级别;你可以围绕添加,删除和引发事件编写一些代码,在这些代码中你可以强制执行规则,并调整会发生什么。例如,您可能希望限制附加到事件的事件处理程序的数量。要实现这一点,您可以更改上面示例中的AddHandler部分:

    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.

如果您不需要这种详细控件,我认为无需声明自定义事件。

#2


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

如果你想要提到你提到的另一篇文章中的相同内容,这里是VB.NET的等价物:

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...

但我认为这不是一个好主意,因为事件的sender参数将是Button,而不是UserControl ......

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

相反,您可以订阅Button.Click事件并引发您自己的事件(没有显式访问者)