Creating a Custom Event

Declare the class containing the event: class MyClass { public event EventHandler MyEvent; public void Method() { OnEvent(); } private void OnEvent() { if (MyEvent != null) { MyEvent(this, EventArgs.Empty); } } } Use it like this: MyClass myObject = new MyClass(); myObject.MyEvent += new EventHandler(myObject_MyEvent); myObject.Method();

modify event data during propagation

The event object that is passed by jQuery is a wrapper around the native event object. The bubbling happens at the javascript/browser level and hence the native event object seems to be shared among all the event handlers. $(‘div’).click(function(e) { e.originalEvent.attribute = (e.originalEvent.attribute || “”) + ” ” + $(this).prop(“className”); alert(e.originalEvent.attribute); }); Tested in Chrome, … Read more

Prevent default behavior in text input while pressing arrow up

To preserve cursor position, backup input.selectionStart before changing value. The problem is that WebKit reacts to keydown and Opera prefers keypress, so there’s kludge: both are handled and throttled. var ignoreKey = false; var handler = function(e) { if (ignoreKey) { e.preventDefault(); return; } if (e.keyCode == 38 || e.keyCode == 40) { var pos … Read more