/// <summary> /// 用户控件 /// </summary> public partial class UCMyControl : UserControl { //定义委托 //EventArgs 可以自己定义参数的类型,一般情况下定义为(object sender,EventArgs e) public delegate void SelectedValueChanged(object sender, EventArgs e); //定义事件 public event SelectedValueChanged SelectChangen; //当用户控件上的某个事件被触发的时候(或者进行某种操作的时候),触发自定义事件 private void UCDDLPLDataAction_SelectedValueChanged(object sender, EventArgs e) {
if (SelectChangen != null)
{
SelectChangen(this,new EventArgs());
} } }
/// <summary> /// 调用 /// </summary> public class UseUC { public UseUC() { UCMyControl uc = new UCMyControl(); //添加事件 uc.SelectChangen += new SelectedValueChanged(SelectedValueChanged); } public void SelectedValueChanged(object sender,EventArgs e) { MessageBox.Show("Test"); } }