如果您创建一个类并将您自己的事件添加到其中,您会得到更好的。
默认的“e”是 System.EventArgs 的一个实例,您可以从中派生出您自己的实例,例如
public class MyEventArgs : System.EventArgs
{
public string EventData {get; private set;}
public MyEventArgs(String argEventData)
{
EventData = argEventData;
}
}
然后在课堂上使用上述内容
public class SomeFellaWithAnEvent
{
public event EventHandler<MyEventArgs> OnMyEvent;
private int _value;
public int Value
{
get {return _value;}
set
{
if (_value != value)
{
_value = value;
DoEvent(_value.ToString();
}
}
}
protected void DoEvent(String argData)
{
if (OnMyEvent != null)
{
OnMyEvent(this,new MyEventArgs(argData))
}
}
}
所以现在你有了一些东西,如果 Value get 发生了变化,如果你给它一个处理程序,它会引发一个事件
例如
SomeFellaWithAnEvent o = new SomeFellaWithAnEvent()
o.OnMyEvent += someThingChanged();
o.Value = 22;
private void somethingChanged(Object sender, MyEventArgs e)
{
// do something with it.
// debug this and you'll see sender is the o instance above, and e is the instance
// of MyEventArgs created in the DoEvent method and has a property set to "22"
}
从工具箱向现有控件添加更多事件处理程序。单击属性检查器中的事件选项卡(闪电按钮),然后双击该值。
或在代码视图中键入 label1。单击 +=,然后按两次 Tab。