function hazCosasMaravillosasConElDOM() {}
//addEvent() by John Resig
function addEvent( obj, type, fn ){
if (obj.addEventListener){
obj.addEventListener( type, fn, false );
}
else if (obj.attachEvent){
obj["e"+type+fn] = fn;
obj[type+fn] = function(){ obj["e"+type+fn]( window.event ); }
obj.attachEvent( "on"+type, obj[type+fn] );
}
}
addEvent(window, 'load', hazCosasMaravillosasConElDOM);
Tag Archive for EventHandler
add event
Generic Based Event Handling Demo for VS.Net 2005
//Define a custom EventArgs class to contain your data.
public class MyEventArgs : EventArgs
{
public string info = "data";
}
//Declare the event as visible to users of the class
public event EventHandler<MyEventArgs> MyEvent;
//Send the event, note that this will throw a nullref if there are no subscribers
//Internal version prevents outsiders from needing to know about the contents of MyEventArgs
protected virtual void InternalSendMyEvent(CustomEventArgs e)
{
if(MyEvent != null)
{
e.info = "Hello Events!";
//This calls all registered subscribers with the following parameters.
MyEvent(this, e);
}
}
//Public version to allow outsiders to trigger the event, not typical implementation.
public void CreateEvent()
{
InternalSendMyEvent(new MyEventArgs());
}
//Consumer
//Register the Handler
eventRaiser.MyEvent += new EventHandler<CustomEventArgs>(HandleEvent);
//Define the Handler
private void HandleEvent(object sender, MyEventArgs e)
{
MessageBox.Show("Event handled:" + e.info);
}
//Cause the Event
eventRaiser.CreateEvent();