如何公开和引发 vb.net winforms 用户控件的自定义事件
请阅读此内容 帖子。 我遇到了与这篇文章中描述的相同的问题,但我尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要使用自定义事件冒泡另一个控件的事件,您可以这样做:
在
AddHandler
和RemoveHandler
中,您只需将附加或删除给定事件处理程序的调用传播到/来自控件的Click
事件。为了扩展一下自定义事件的使用,这里是自定义事件的另一个示例实现:
现在,正如上面所示,它除了常规事件声明之外几乎没有其他作用:
它提供了类似的机制,允许附加事件处理程序并删除,并引发该事件。 自定义事件添加的是额外的控制级别; 您可以围绕添加、删除和引发事件编写一些代码,在其中您可以强制执行规则,并稍微调整将要发生的情况。 例如,您可能希望限制附加到事件的事件处理程序的数量。 为了实现这一点,您可以更改上面示例中的
AddHandler
部分:如果您不需要这种详细的控制,我认为不需要声明自定义事件。
To use custom events for bubbling the events of another control, you can do like this:
In
AddHandler
andRemoveHandler
you simply propagate the call to attach or remove the given event handler to/from the control'sClick
event.To expand a bit on the use of custom events, here is another sample implementation of a custom event:
Now, as it looks above, it does little else than a regular event declaration:
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:If you don't need that kind of detailed control, I see no need to declare custom events.
如果您想要与您提到的其他帖子中相同的东西,这里有 VB.NET 等效项:
但我认为这不是一个好主意,因为事件的
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 :
But I don't think that's a good idea, because the
sender
parameter of the event will be theButton
, not yourUserControl
...Instead, you could subscribe to the Button.Click event and raise you own event (with no explicit accessors)