Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain code for custom event in user control

Someone gave me this code that works great. But I would really like to understand what is happening inside it. Could somebody explain please? What is the meaning of each part of the code? The code is inside a custom control which has two labels inside a panel.

Also I've seen some custom control events that use add/remove syntax, what is that for? What is the difference with what is happening here?

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public event EventHandler MyCustomClickEvent;

    protected virtual void OnMyCustomClickEvent(EventArgs e)
    {
        // Here, you use the "this" so it's your own control. You can also
        // customize the EventArgs to pass something you'd like.

        if (MyCustomClickEvent != null)
            MyCustomClickEvent(this, e);
    }

    private void label1_Click(object sender, EventArgs e)
    {
        OnMyCustomClickEvent(EventArgs.Empty);
    }
}
like image 694
VerizonW Avatar asked Oct 22 '10 15:10

VerizonW


People also ask

What is custom control user control?

Custom control is a generic term that also includes user controls. User control in ASP.NET is created using ASP.NET code and is reused in other Web pages, whereas user control in the context of Windows Forms implies a composite control with a consistent user interface (UI) and behavior within or across applications.

What is user control how it is created explain its Events & Limitations briefly?

A User Control is a reusable page or control with an extension of . ascx and created similar to an . aspx page but the difference is that a User Control does not render on its own, it requires an . aspx page to be rendered. User Controls are very useful to avoid repetition of code for similar requirements.

What are events in C# with example?

Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence such as system generated notifications. Applications need to respond to events when they occur. For example, interrupts.


1 Answers

See my comments below. Also for a more detailed event I blogged on this concept a while back where I go into more detail on the entire process.

public partial class UserControl1 : UserControl
{
    //This is the standard constructor of a user control
    public UserControl1()
    {
        InitializeComponent();
    }

    //This defines an event called "MyCustomClickEvent", which is a generic
    //event handler.  (EventHander is a delegate definition that defines the contract
    //of what information will be shared by the event.  In this case a single parameter
    //of an EventArgs object.
    public event EventHandler MyCustomClickEvent;


    //This method is used to raise the event, when the event should be raised, 
    //this method will check to see if there are any subscribers, if there are, 
    //it raises the event
    protected virtual void OnMyCustomClickEvent(EventArgs e)
    {
        // Here, you use the "this" so it's your own control. You can also
        // customize the EventArgs to pass something you'd like.

        if (MyCustomClickEvent != null)
            MyCustomClickEvent(this, e);
    }

    private void label1_Click(object sender, EventArgs e)
    {
        OnMyCustomClickEvent(EventArgs.Empty);
    }
}
like image 178
Mitchel Sellers Avatar answered Oct 20 '22 09:10

Mitchel Sellers