Custom Control Events in C#

Posted by pm_2 on Stack Overflow See other posts from Stack Overflow or by pm_2
Published on 2010-05-01T14:25:13Z Indexed on 2010/05/01 14:37 UTC
Read the original article Hit count: 193

Filed under:
|
|
|

I'm trying to create a custom control and need to raise an event from it. The idea is to raise an event at the end of the click event (OnAfterClick). I found one or two tutorials on doing this, but am clearly missing a step somewhere; I have the following.

In the control:

public class AfterClickEventArgs : EventArgs
{
  ...
}

public partial class MyButton : CommandButton
{
    public delegate void AfterClickEvnt(object sender, AfterClickEventArgs e);

    public event AfterClickUpdatedEvnt AfterClick;

}

protected override void OnClick(EventArgs e)
{
    ...
    Processing here
    ...

    AfterClickEventArgs myArgs = new AfterClickEventArgs();                
    AfterClick(this, newArgs);
}

In the program using the control:

In InitializeComponent():

this.MyButton.AfterClick += new System.EventHandler(this.cmdMyButton_Click);

This line is giving me a compile error (cmdMyButton_Click does exist). It tells me: Cannot implicitly convert type 'System.EventHandler' to 'Namespace.MyButton.AfterClick'

Can anyone tell me what I'm missing, or misunderstanding about this, please?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET