Event triggering an event

I have an object that is private in my class. If this object fires an event, I want to pass the event to everything that is used by my class. Currently, I am doing it like this, I have introduced my constructor:

cbName.CheckedChanged += ((sender, args) => this.CheckChanged(this,args));

Is there a better way to do this, and are there any errors, such as a class, that will not be deleted, because it has an event for itself, and I will need to manually unsubscribe from the delete function?

Changing the sender from the firing object to thisis optional.

Full version of test code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace placeholder
{
    internal class FilterBase : UserControl, IFilterObject
    {
        public FilterBase(string name)
        {
            InitializeComponent();
            cbName.CheckedChanged += ((sender, args) => this.CheckChanged(this,args));
            cbName.Name = name;
            this.Name = name;
        }

        private CheckBox cbName;
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.cbName = new System.Windows.Forms.CheckBox();
            this.SuspendLayout();
            // 
            // cbName
            // 
            this.cbName.AutoSize = true;
            this.cbName.Location = new System.Drawing.Point(4, 4);
            this.cbName.Name = "cbName";
            this.cbName.Size = new System.Drawing.Size(79, 17);
            this.cbName.TabIndex = 0;
            this.cbName.Text = "Filter Name";
            this.cbName.UseVisualStyleBackColor = true;
            // 
            // UserControl1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoSize = true;
            this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.Controls.Add(this.cbName);
            this.Name = "Filter Name";
            this.Size = new System.Drawing.Size(86, 24);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        public event EventHandler CheckChanged;
        public bool Checked
        {
            get { return cbName.Checked; }
        }
    }
}
+3
source share
2 answers

An event -. add remove, , , :

public event EventHandler CheckChanged {
    add { cbName.CheckChanged += value; }
    remove { cbName.CheckChanged -= value; }
}

Delegate, ( Delegate )

+3

, , cbName , , ... , this ( CheckChanged ).

, (, Dispose), . :

private void CbNameCheckedChangedHandler(object sender, EventArgs e)
{
    CheckedChanged(this, args);
}

...
cbName.CheckedChanged += CbNameCheckedChangedHandler;

// If you ever want to remove the handler
cbName.CheckedChanged -= CbNameCheckedChangedHandler;

, , CheckedChanged , . :

public event EventHandler CheckedChanged = delegate {};

, , NullReferenceException, cbName.CheckedChanged.

+1

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


All Articles