熟悉C#的同学们,都知道事件的好处,在我们刚刚学习编程的时候就知道拖个按钮,点击事件,就是事件的入门,在鼠标点击后我们就能完成我们想要的结果,例如 弹出一个新的窗体,闲话休说,上代码
一. 声明一个事件委托类
/// <summary>
/// 委托事件类
/// </summary>
/// <param name="e"></param>
public delegate void Counthandler(CountEventArgs e);
二 将委托绑定到事件上
public event Counthandler counts;
三 细化事件所需参数 e
public class CountEventArgs : EventArgs
{
public int count { get; set; }
}
四 在所需方法中调用事件 并传入参数为方便将
<pre class="csharp" name="code">private void timer1_Tick(object sender, EventArgs e)
{
i++;
ex.count = i;
counts(ex);
}
五 订阅或注册事件
counts += new Counthandler(Form1_counts);
六 完成我们需要做的事情
<pre class="csharp" name="code"> void Form1_counts(CountEventArgs e)
{
this.Text = e.count.ToString(); ;
}