Explain code for user event in user control

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

I also saw some user control events that use the add / remove syntax, what is this for? What is the difference with what's 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 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); } } 
+7
c # events winforms user-controls
source share
4 answers

See my comments below. In addition, for a more detailed event, I blogged in this concept a while ago, where I will talk more about this 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 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); } } 
+8
source share

I would recommend reading Events for C # on MSDN . This is described in detail.

Basically, MyCustomClickEvent is an event. The OnMyCustomClickEvent method OnMyCustomClickEvent used to raise an event, but is performed in such a way that subclasses can also raise this event, if necessary.

When you click "label1", the OnMyCustomClickEvent method is OnMyCustomClickEvent , which raises the event. Any delegates who sign up for this event will be running at that moment.

+2
source share

You mentioned the syntax for adding / removing events in some user control examples. Most likely, these examples using the UserControl class' Events property to store event handlers, for example, in the following example:

  public event EventHandler MyEvent { add { Events.AddHandler("MyEvent", value); } remove { Events.RemoveHandler("MyEvent", value); } } 

The idea is that usually the user of the control does not want to handle every event that the control provides. If each event is defined as a "field" event (as in your example), then each event will occupy a piece of memory, even if there are no subscribers for this event. When you have a complex page consisting of hundreds of controls, each of which can have dozens of events, the memory consumption for unused events is not negligible.

This is why the System.ComponentModel.Component class (the base class of the System.Windows.Forms.Control class) has the Events property, which is basically a dictionary for storing event handler delegates. Thus, each event is implemented more as a property than a field. Add / remove handlers for each event store or remove delegates from the Events dictionary. If an event is not used, then there is no word for it in the Events dictionary, and additional memory is not consumed for this event. This is a compromise in that you do a little more work (you need to look for an event handler) to save a little more memory.

EDIT: my answer is for Windows Forms, not ASP.NET, although the concepts are the same.

+1
source share

As for adding / removing, this is a β€œmanual” implementation of events. The following two snippets do the same.

Automatic implementation:

 public event EventHandler MyEvent; 

Manual implementation:

 private EventHandler _myEvent; public event EventHandler MyEvent { add { _myEvent += value; } remove { _myEvent -= value; } } 

This is exactly the same idea as automatic properties, where:

 public string Property { get; set; }; 

In the same way as:

 private string _property; public string Property { get { return _property; } set { _property = value; } } 

The difference between these fragments is that with manual implementations you get more control. Examples are:

  • Logic implementation in add / get and remove / set;
  • Get access to fields that you can set, for example. [NonSerializable];
  • Put the values, for example. a Dictionary .

Form class, for example. the latter stores the number of fields in the form class.

0
source share

All Articles