1.首先定义一个参数类
public partial class SayEventArgs : EventArgs
{
public String Username;
public String Websiteurl;
}
{
public String Username;
public String Websiteurl;
}
2.定义一个委托
public delegate void SayEventHandler(object sender,SayEventArgs e);
3.定义一个事件
public event SayEventHandler SayEvent;
4.引发事件
public void OnSayEvent(SayEventArgs e)
{
if (SayEvent != null)
{
SayEvent(this, e);
}
}
{
if (SayEvent != null)
{
SayEvent(this, e);
}
}
5.传参函数
public void Loginsite(String Username, String Websiteurl)
{
SayEventArgs e = new SayEventArgs();
e.Username = Username;
e.Websiteurl = Websiteurl;
Label1.Text = e.Username + "<br>";
Label1.Text += "欢迎你来到<a href=" + e.Websiteurl + ">痞子菜雨夜听风小轩</a>";
OnSayEvent(e);
}
{
SayEventArgs e = new SayEventArgs();
e.Username = Username;
e.Websiteurl = Websiteurl;
Label1.Text = e.Username + "<br>";
Label1.Text += "欢迎你来到<a href=" + e.Websiteurl + ">痞子菜雨夜听风小轩</a>";
OnSayEvent(e);
}
6.所要委托的函数
public void Saysomething(object sender, SayEventArgs e)
{
Label2.Text = e.Username+"请注意:<br>在呢个博客<a href=" + e.Websiteurl + ">痞子菜雨夜听风小轩</a>度,唔系事必要你讲野,但系你所讲既野将来会作为呈堂证供,ok?";
}
{
Label2.Text = e.Username+"请注意:<br>在呢个博客<a href=" + e.Websiteurl + ">痞子菜雨夜听风小轩</a>度,唔系事必要你讲野,但系你所讲既野将来会作为呈堂证供,ok?";
}
7.建立委托实例
SayEvent += new SayEventHandler(Saysomething);
Loginsite("痞子菜", "u/admin/index.html");
Loginsite("痞子菜", "u/admin/index.html");
看到了吗?定义事件也就这几步,要复杂点的话就把函数写得复杂点就行了。就这样。