您的位置:首页 > 其它

如何给自定义控件添加事件

2009-03-30 08:18 204 查看
代碼如下:

#region 为复合控件添加事件
//声明复合控件的事件
public event DataGridViewCellEventHandler CellClick;
//委托处理的事件代码
protected virtual void OnCellClick(DataGridViewCellEventArgs e)
{
DataGridViewCellEventHandler dg = CellClick;
//如果事件不為空
if (dg != null)
{
dg(dataGV, e);//調用事件
}
}
#endregion

#region 复合函数的构造函数
/// <summary>
/// 构造函数
/// </summary>
public UserDataGridView()
{
InitializeComponent();
//为CellClick事件绑定一个委托事件
dataGV.CellClick += delegate(object sender, DataGridViewCellEventArgs e)
{
OnCellClick(e);
};
}
#endregion


在自定义控件中这样声明以后就可以直接在使用的地方为其写CellClick事件代码了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: