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

发布于 2024-07-26 06:52:24 字数 893 浏览 9 评论 0原文

请阅读此内容 帖子。 我遇到了与这篇文章中描述的相同的问题,但我尝试在 VB.net 而不是 c# 中执行。

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

Public Custom Event AddRemoveAttendees As EventHandler

它会扩展为以下代码片段。

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

但我不知道该怎么办。 直到今天我还从未听说过自定义事件。

我想要的底线是让按钮的单击事件冒泡到用户控件的容器。 我知道我可以包装自己的事件,但在我沿着这条路走得更远之前,我至少想了解自定义事件。

赛斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回旧景 2024-08-02 06:52:24

要使用自定义事件冒泡另一个控件的事件,您可以这样做:

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

AddHandlerRemoveHandler 中,您只需将附加或删除给定事件处理程序的调用传播到/来自控件的 Click 事件。

为了扩展一下自定义事件的使用,这里是自定义事件的另一个示例实现:

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

现在,正如上面所示,它除了常规事件声明之外几乎没有其他作用:

Public Event AddRemoveAttendees As EventHandler

它提供了类似的机制,允许附加事件处理程序并删除,并引发该事件。 自定义事件添加的是额外的控制级别; 您可以围绕添加、删除和引发事件编写一些代码,在其中您可以强制执行规则,并稍微调整将要发生的情况。 例如,您可能希望限制附加到事件的事件处理程序的数量。 为了实现这一点,您可以更改上面示例中的 AddHandler 部分:

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

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

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.

甜中书 2024-08-02 06:52:24

如果您想要与您提到的其他帖子中相同的东西,这里有 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

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

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

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文