Add delegate or event property to this class?

I created a control in which other developers can instantiate and use it. The control has a button. How do I let developers plug in their own code in a specific part of my control? I am not sure how and how to use a delegate or event in this scenario. Can anyone help in the example below:

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

In developer code, how can they go through their logic when a button click on a control succeeds?

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 update MyControl to allow this?

+3
source share
4 answers

You need to add a control, for example:

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);
}

, :

myControl.SomethingCompleted += myControl_SomethingCompleted;

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

. .

+5

EventHandler, EventHandler. .

!

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 LOGIC

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

!

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
}

!

+1

args .

- , .

0

.

- :

public class MyControl : CompositeControl
{
       public event EventHandler Success;
       ...
       void btnSubmit_Click(object sender, EventArgs e)
   {   
       //DO SOMETHING
        if (success)
        {
             //CALL DEVELOPER 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.
         }
    }
}

:

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

    ctlMyConstrol += SuccessCode;

    this.Control.Add(ctlMyControl);
}

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

MSDN .

0

Source: https://habr.com/ru/post/1767880/


All Articles