PDA

View Full Version : چطور میتونیم Event تعریف کنیم؟



Developer Programmer
چهارشنبه 30 بهمن 1387, 13:42 عصر
وقتی که داریم یه کلاس مثل TShape مینویسیم. چطور میتونیم یک متد رو از نوع Event تعریف کنیم؟ تا بعد اون رو هندل کنیم؟

Ali_Mor
چهارشنبه 30 بهمن 1387, 15:21 عصر
فرض کنیم می خوای در پایان عملیات Load یک اونت رخ بده


public class TShape
{

public event CopmpleteEventHandler Copmplete;
public delegate void CopmpleteEventHandler();

public void load()
{
//...
//...
//...

if (Copmplete != null) {
Copmplete();
}
}
}


حالا در برنامه اینجوری ازش استفاده کن


private void Form1_Load(object sender, EventArgs e)
{
TShape test = new TShape();
test.Copmplete += Complete_Handel;

test.load();
}

private void Complete_Handel()
{
MessageBox.Show ("load complete ");
}