Niiw Niiw - 2 months ago 12
C# Question

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.

How could I add a custom event to that control ?
I'd like to do a custom Event which fires when "textBoxState" is changed.

Answer
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. after declaring and calling it in setter of your property you can use outside of this class by instancing and declaring this event.