简体   繁体   English

C#修改控件自定义Event

[英]C# Modified control custom Event

Let's say I have a modified control of a textbox假设我有一个文本框的修改控件

public class resButton : TextBox
{

    [Browsable(true)]


    [Description("state of TextBox"), Category("Data")]
    public string textBoxState
    {
        get { return this.AccessibleDescription; }
        set {
            this.AccessibleDescription = value;
            }
    }
}

And I have added a custom propertie which is based on the AccessibleDescription of that control.我添加了一个基于该控件的 AccessibleDescription 的自定义属性。

How could I add a custom event to that control ?如何向该控件添加自定义事件? I'd like to do a custom Event which fires when "textBoxState" is changed.我想做一个自定义事件,当“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
    }
}

you must declare event like this sample with existing delegate(EventHandler) or your custom delegate.您必须使用现有委托(EventHandler) 或您的自定义委托声明类似此示例的事件。 after declaring and calling it in setter of your property you can use outside of this class by instancing and declaring this event.在您的属性的 setter 中声明并调用它之后,您可以通过实例化和声明此事件在此类之外使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM