开发者

What's equivalent to javascript bubbling events in .NET?

I think Javascript has a mechanism that allows to bubbling events from top down and down up. What's the equivalent in .NET WINFORM (not ASP.NET) ?

Events/Delegates in .NET automates the Observer/Design Pattern by avoiding the subject to handle the message update sending to subscribers so I can't see any fundamental reason why bubling couldn't have existed.

Events/Delegates allows loose coupling compared to using IObserver, in the same way if bubbling was implemented it would also allow loose coupling instead of doing inheritance and 开发者_运维百科hard-wire the call to the base parent.


Hi you can do this in .net

see this article

and this


This has very little to do with JavaScript, bubbling is a property of the DOM. Which represents documents in a tree-like hierarchy, making it natural to pass events that are not handled up the tree.

This doesn't nearly correspond as well in a window hierarchy. Chief problem being that if you nest windows deeply then you'd have been yourself a dog of a program that takes forever to draw the UI. Nevertheless, the default window procedure does bubble window messages to the parent. This is selective behavior, it depends on the particular message. A WM_MOUSEWHEEL message for example bubbles, looking for a parent window that knows how to scroll the view. But WM_LBUTTONDOWN does not bubble, the parent window wouldn't normally have much use for a mouse click on an area that it doesn't 'own'. Other than perhaps to set the focus to the control, something that already happens automatically.

You can certainly make it bubble yourself, just send the message to the parent. In effect this already happens. A control normally generates a MouseDown or Click event. Which is subscribed by an event handler in the form. Different model, same effect.


There's no such thing. If you want to pass the event along to your parent, use the sender parameter to your event, cast it to a Control, call the Parent method, and fire the event there.

That said, you probably shouldn't do this. The .NET platform is not Javascript. When in Rome, do as the romans do.

Really, events in .NET are nothing like Javascript events at all... they are merely a list of delegates. Raising the event results in all of the delegates being called. Events can be placed on all objects, not only GUI objects. Therefore it would make no sense to define them as solely a GUI concept.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜