Implementing custom property changes through C # Triggers custom events, understanding custom events and their triggering processes

Source: Internet
Author: User

The following instructions explain the custom triggering process for custom events:


Directly on the code, containing instructions (interface is two text box Textbox1,textbox2, and a Button1, interface Load event, button's Click event)


Form1 Class (caller side)
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing;  Using system.linq;using system.text;using System.Windows.Forms;         namespace test{public partial class Form1:form {public Form1 () {InitializeComponent (); }//customclass cc = new Customclass (1, "Lee");//test the property value does not change the situation customclass cc = new Customclass ();// Null constructor, side test property value change private void Form1_Load (object sender, EventArgs e) {            cc. Changed + = new Customclass.changedeventhandler (cc_changed);//caller-side subscription event provides a specific event function for the Changed event cc_changed private void Button1_Click (object sender, EventArgs e) {           cc. Cid = 1;cc. Cname = "Lee";//Assign a value to the Customclass attribute, which is the event that is raisedString str = cc. Cid.tostring () + CC.            Cname;        MessageBox.Show (str); }private voidcc_changed ()//Event Note: Event functions bound by custom events{TextBox1.Text = cc.            Cid.tostring (); TextBox2.Text = cc.        Cname; }    }Customclass Class (event definition side)
public class Customclass {public delegate void Changedeventhandler ();//define Delegate public event Changedeventhan        Dler changed;//define event private int _cid;          private string _cname; Public Customclass () {}
        public customclass (int cCid, string ccname)         {      & nbsp     THIS._CID = ccid;            This._cname = ccname;        }          protected virtual void OnChanged ()         {    &NBS P       if (changed!=null)             {          &NBSP ;     Changed ();           }       }          public int cid        {            get            {                return _cid;        &NB Sp  }            set            {      &NBSP ;        if (_cid!=value)//Here is the process of text change                 {    & nbsp               _CID = value;                    OnChanged ();//Start Event//Note: In disguise in changing the value of the process, called the above bound event function "cc_changed ()", to the custom event trigger (cc_changed () Illusion execution ) //need to understand: in the current classCustomclass, you cannot directly know the event function to execute                   //or so: in the class that defines the custom event, simply define (1) the event itself and (2) the event invocation that may be triggered in the class (In this class, the onchanged () changed () ), which event function is called, is provided by the caller-side }}} public string Cname {get {return _cname; } set {if (_cname! = value) {_cname = VA                    Lue                OnChanged (); }            }        }    }}
The following is an example of a very classic property value change on the web that raises a custom event, as follows;public class MyClass {public event eventhandler<propertychagedeventargs> mypropertychanging; public event eventhandler<propertychagedeventargs> Mypropertychanged;
        private int _myproperty;        public int myproperty        {            get {return _myproperty;}             set            {                if (value! = _myproperty)                 {    &NBS P               Propertychagedeventargs e = new Propertychagedeventargs ("MyProperty", _my property, value),//Initialize                     if (this. Mypropertychanging! = null)                     {      &NB Sp                 this. Mypropertychanging (this, e);                        if (E.canc EL) return;  &nbsP                }                  &N Bsp _myproperty = (int) e.newvalue;                    if (this. Mypropertychanged! = null)                     {      &NBS P                 this. Mypropertychanged (this, e);                   }      &NB Sp        }           }       }   }     //<summary>   //General class    //</summary>    public class Proper tychagedeventargs:eventargs    {        public Propertychagedeventargs (string Propertyname,object oldvalue,object newvalue)         {           propertyname = propertyname;            OldValue = oldvalue;  &nbs P         NewValue = newvalue;       }         public bool Cancel {get; set;}         public string PropertyName {get; private set;}         public object OldValue {get; private set;}         public object NewValue {get; set;}    }

Implementing custom property changes through C # Triggers custom events, understanding custom events and their triggering procedures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.