C# 中的
要在程序中使用自定義事件
要在類內聲明事件
public delegate void SelectionChangedEventHandler(object sender
SelectionChangedEventArgs e);
說明
public class SelectionChangedEventArgs : EventArgs
{
private string m_selection;
//本屬性用於傳遞事件數據
public string Selection
{
get { return m_selection; }
}
public SelectionChangedEventArgs(string selection)
{
m_selection = selection;
}
}
委托類型定義傳遞給處理該事件的方法的一組參數
接下來
public event SelectionChangedEventHandler SelectionChanged;
如果沒有任何客戶將委托與該事件掛鉤
public class Form
{
public event SelectionChangedEventHandler SelectionChanged;
private void comboBox
{
if (SelectionChanged != null)
{
SelectionChangedEventArgs E = new SelectionChangedEventArgs(comboBox
SelectionChanged(this
}
}
}
為了開始接收事件調用
public class Form
{
private Form
public Form
{
m_frm
//與事件掛鉤
m_frm
}
private void button
{
m_frm
}
private void frm
{
//獲取事件傳遞過來的數據
textBox
}
}
本示例效果如下圖所示
From:http://tw.wingwit.com/Article/program/ASP/201311/21673.html