如何公开和提升vb.net winforms用户控件的自定义事件 [英] How to expose and raise custom events for a vb.net winforms user control

查看:354
本文介绍了如何公开和提升vb.net winforms用户控件的自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请阅读此信息帖子。我有与这篇文章中描述的相同的问题,但我试图在VB.net而不是c#。

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

我很确定这样做,我必须使用自定义事件。 (我使用代码转换网站了解自定义事件)所以在IDE中,当我键入以下内容:

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:

公共自定义事件AddRemoveAttendees作为EventHandler

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

推荐答案

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

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

AddHandler RemoveHandler 中,您只需传播呼叫即可附加或删除给定的事件处理程序返回/从控件的单击事件。

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

它提供了类似的机制,允许附加和删除事件处理程序,事件要提出来自定义事件添加的是一个额外的控制;您可以在添加,删除和提升事件时编写一些代码,您可以在其中强制执行规则,并调整会发生什么。例如,您可能希望限制附加到事件的事件处理程序的数量。要实现这一点,您可以从上面的示例中更改 AddHandler 部分:

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.

这篇关于如何公开和提升vb.net winforms用户控件的自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆