Skip to content

Instantly share code, notes, and snippets.

@afrael
Created July 18, 2011 14:17
Show Gist options
  • Save afrael/1089636 to your computer and use it in GitHub Desktop.
Save afrael/1089636 to your computer and use it in GitHub Desktop.
Quick and dirty .NET user event creation and handling
// BEGIN - create your argument class, subclass of EventArgs
public class ProgressEventArgs : EventArgs
{
public int Min;
public int Progress;
public int Max;
public string Message;
public ProgressEventArgs(int min, int progress, int max, string msg)
{
if (progress > max)
progress = max;
Min = min;
Progress = progress;
Max = max;
Message = msg;
}
}
// END
// BEGIN -- create your event delegate in the class where you want to trigger the event
public delegate void ProgressEventDelegate(ProgressEventArgs e);
// END
// BEGIN -- declare trigger your event
// declare in your class as so
public event ProgressEventDelegate progressEvent;
progressEvent.Invoke(new ProgressEventArgs(0, iProgress, 10, "Beginning refresh."));
// END
// BEGIN -- trap (handle) your event, usually in your UI
myObj.progressEvent += new ProgressEventDelegate(myObj_progressEvent);
private void myObj_progressEvent(ProgressEventArgs e)
{
// your custom event handling code
}
// END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment