TruMan1
TruMan1

Reputation: 36078

Add delegate or event property to this class?

I created a control where other developers can create an instance and use. There is a button click in the control. How do I allow developers to plug in their own code in a certain part of my control? I am not sure if or how exactly to use a delegate or event in this scenario. Can someone help in the below example:

public class MyControl : CompositeControl
{
   ...
   void btnSubmit_Click(object sender, EventArgs e)
   {
      //DO SOMETHING
      if (success)
      {
         //CALL DEVELOPER'S LOGIC
      }
   }
}

In the developers code, how can they pass in their logic when the button click of the control is successful?

protected override void CreateChildControls()
{
    var ctlMyControl = new MyControl();

    //ADD SuccessCode() TO ctlMyControl
    //SO IT IS TRIGGERED ON SUCCESS
    //MAYBE SOMETHING LIKE:
    //ctlMyControl.SuccessEvent = SuccessCode()??

    this.Control.Add(ctlMyControl);
}

protected void SuccessCode()
{
    //DO SOMETHING
}

How to I update MyControl to allow this?

Upvotes: 3

Views: 1414

Answers (4)

Doug
Doug

Reputation: 5318

Since your event uses the default signature of type EventHandler, you just need to expose a public event of type EventHandler. So take a look at the code below.

Here is how you implement the new event!

public class MyControl : CompositeControl
{
   //New public event declaration of type EventHandler
   public event EventHandler Submit_OnClick;

   void btnSubmit_Click(object sender, EventArgs e)
   {
      //DO SOMETHING
      if (success)
      {
         //CALL DEVELOPER'S LOGIC

         //Code to raise the event only if there are subscribers.
         if (Submit_OnClick != null)
         {
           Submit_OnClick(this, new EventArgs());
         }
      }
   }
}

Here is how you use the new event!

protected override void CreateChildControls()
{
    var ctlMyControl = new MyControl();
      //Subscribed to the newly added event of your MyControl class
        ctlMyControl.Submit_OnClick += new EventHandler(SuccessCode);

    //ADD SuccessCode() TO ctlMyControl
    //SO IT IS TRIGGERED ON SUCCESS
    //MAYBE SOMETHING LIKE:
    //ctlMyControl.SuccessEvent = SuccessCode()??

    this.Control.Add(ctlMyControl);
}

//Modified the signature to be compliant with EventHandler type
protected void SuccessCode(object sender, EventArgs e)
{
    //DO SOMETHING
}

Enjoy!

Upvotes: 1

Vyacheslav Antonyuk
Vyacheslav Antonyuk

Reputation: 119

You should add an event to the your control.

Something like this:

public class MyControl : CompositeControl
{
       public event EventHandler Success;
       ...
       void btnSubmit_Click(object sender, EventArgs e)
   {   
       //DO SOMETHING
        if (success)
        {
             //CALL DEVELOPER'S LOGIC
             OnSuccess();
        }
   }

    private static void OnSuccess()
    {
         if(Success != null)
         {
              Success(this, EventArgs.Empty); //or you can pass event args you want. But you should use EventHandler<T> instead.
         }
    }
}

And than other developers can youse your control like this:

protected override void CreateChildControls()
{
    var ctlMyControl = new MyControl();

    ctlMyConstrol += SuccessCode;

    this.Control.Add(ctlMyControl);
}

protected void SuccessCode(object sender, EventArgs e)
{
    //DO SOMETHING
}

You can read more about events on MSDN.

Upvotes: 0

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

You will need to declare the event, and if necessary create a custom event args class for passing information about the event.

Here is a blog post I wrote a while back that walks through the process.

Upvotes: 0

SLaks
SLaks

Reputation: 887415

You need to add an event to the control, like this:

void btnSubmit_Click(object sender, EventArgs e) {
    //DO SOMETHING
    if (success) {
        OnSomethingCompleted();
    }
}

///<summary>Occurs when the operation is successfully completed.</summary>
public event EventHandler SomethingCompleted;
///<summary>Raises the SomethingCompleted event.</summary>
internal protected virtual void OnSomethingCompleted() { OnSomethingCompleted(EventArgs.Empty); }
///<summary>Raises the SomethingCompleted event.</summary>
///<param name="e">An EventArgs object that provides the event data.</param>
internal protected virtual void OnSomethingCompleted(EventArgs e) {
    if (SomethingCompleted != null)
        SomethingCompleted(this, e);
}

In the form, you can add a handler to the event, like this:

myControl.SomethingCompleted += myControl_SomethingCompleted;

void myControl_SomethingCompleted(object sender, EventArgs e) {
    //Do things
}

For more information, see the documentation.

Upvotes: 5

Related Questions