[英]C# Modified control custom Event
假設我有一個文本框的修改控件
public class resButton : TextBox
{
[Browsable(true)]
[Description("state of TextBox"), Category("Data")]
public string textBoxState
{
get { return this.AccessibleDescription; }
set {
this.AccessibleDescription = value;
}
}
}
我添加了一個基於該控件的 AccessibleDescription 的自定義屬性。
如何向該控件添加自定義事件? 我想做一個自定義事件,當“textBoxState”改變時觸發。
public class resButton : TextBox
{
[Browsable(true)]
[Description("state of TextBox"), Category("Data")]
public string textBoxState
{
get { return this.AccessibleDescription; }
set
{
this.AccessibleDescription = value;
if (yourEvent != null)
yourEvent(this, new EventArgs());
}
}
public event EventHandler yourEvent;
}
public class resButtonUsage
{
resButton resbuttonInstance;
public resButtonUsage()
{
resbuttonInstance = new resButton();
resbuttonInstance.yourEvent += resbuttonInstance_yourEvent;
}
void resbuttonInstance_yourEvent(object sender, EventArgs e)
{
// Your implementation
}
}
您必須使用現有委托(EventHandler) 或您的自定義委托聲明類似此示例的事件。 在您的屬性的 setter 中聲明並調用它之后,您可以通過實例化和聲明此事件在此類之外使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.