c#自定义控件中的事件处理

时间:2022-02-01 23:29:26
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace UserControls
{
public partial class UserControl1 : UserControl
{
public delegate void MyDelegate(object sender, EventArgs e);//建立委托
public MyDelegate myEven_click;
public UserControl1()
{
InitializeComponent();
button1.Click += new EventHandler(button1_Click); //绑定委托事件
} private void button1_Click(object sender, EventArgs e)
{
if (myEven_click != null)
{
myEven_click(sender, e);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace 自定义控件的点击事件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
userControl11.myEven_click += new UserControls.UserControl1.MyDelegate(fuzhi);//把事件绑定到自定义的委托上
}
public void fuzhi(object sender ,EventArgs e)
{
label1.Text = "";
}
}
}