A generic
EventArgs derivative that allows creating
different custom EventArgs without subclasses.
System Object System EventArgs CleanCode.Forms EventArg T Namespace: CleanCode.FormsAssembly: CleanCode (in CleanCode.dll) Version: 1.2.3.0 (1.2.03)
public class EventArg<T> : EventArgs
Public Class EventArg(Of T) _
Inherits EventArgs
Public Class EventArg(Of T) _
Inherits EventArgs
generic<typename T>
public ref class EventArg : public EventArgs
generic<typename T>
public ref class EventArg : public EventArgs
- T
- The custom EventArg type.
This class provides a generics framework for handling custom data
when firing an event. It requires only a
struct
to back it up instead of a full-fledged class.
This portion of code goes in the class that will fire the event:
public delegate void FilenameChangedEventHandler(
object sender, EventArg<FilenameChangedData> e);
public struct FilenameChangedData
{
public string filename;
public FilenameChangedData(string filename)
{
this.filename = filename;
}
}
public event FilenameChangedEventHandler FilenameChanged;
private void OnFilenameChanged(string newFileName)
{
if (FilenameChanged != null)
{
FilenameChanged(this,
new EventArg<FilenameChangedData>(
new FilenameChangedData(newFileName)));
}
}
public delegate void FilenameChangedEventHandler(
object sender, EventArg<FilenameChangedData> e);
public struct FilenameChangedData
{
public string filename;
public FilenameChangedData(string filename)
{
this.filename = filename;
}
}
public event FilenameChangedEventHandler FilenameChanged;
private void OnFilenameChanged(string newFileName)
{
if (FilenameChanged != null)
{
FilenameChanged(this,
new EventArg<FilenameChangedData>(
new FilenameChangedData(newFileName)));
}
}
. . .
MyControl.FilenameChanged +=
new FilenameChangedEventHandler(myControl_FilenameChanged);
private void myControl_FilenameChanged(object sender, EventArg<FilenameChangedData> e)
{
this.Text = e.Data.filename;
}
. . .
MyControl.FilenameChanged +=
new FilenameChangedEventHandler(myControl_FilenameChanged);
private void myControl_FilenameChanged(object sender, EventArg<FilenameChangedData> e)
{ // Update the title of the Form with the filename.
this.Text = e.Data.filename;
}
This portion of code goes in the class that will respond to the event:
The idea for this class was adapted from user comments on
http://msdn.microsoft.com/en-us/library/system.eventargs(VS.80).aspx
Since CleanCode 0.9.23.