本文转载自:
https://www.cnblogs.com/binbinxiong/p/3721276.html 作者:binbinxiong 转载请注明该声明。
1,自定义事件:
public class TextChangeEventArgs : EventArgs
{
private string message;
public TextChangeEventArgs(string message)
{
this.message = message;
}
public
string Message
{
get{return message;}
}
}
2,定义委托为,
public
delegate void TextBoxChangedHandle(object sender,TextChangeEventArgs e);
接下去在用户控件中添加事件,
//
定义事件
public
event TextBoxChangedHandle UserControlValueChanged;
为了激发用户控件的新增事件,修改了一下代码,
private
void textBox1_TextChanged(object sender, System.EventArgs e)
{
if(UserControlValueChanged != null)
UserControlValueChanged(this,new TextChangeEventArgs(this.textBox1.Text));
}
3,调用事件
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
if(UserControlValueChanged != null)
UserControlValueChanged(this,new TextChangeEventArgs(this.textBox1.Text));
}