First, click the Saywords button, directly trigger events, display data
Second, start editing the event code, where a Jiafang class and a message (the owning EventArgs, the event argument) are created to encapsulate the properties
1, the Jiafang classnamespacedelegate and event again_and_again{//create a class of party a Public classJiafang {//Add members to party a Private string_word; Public stringWord {Get{return_word;} Set{_word =value;} } Private string_name; Public stringName {Get{return_name;} Set{_name =value;} } //Define an event Public EventWeituohandler events; //define a function to trigger this event Public voidsaywords () {if(events!=NULL) {Events ( This,NewMessage () {word="I am party a"}); } } }}2, the class of messagenamespacedelegate and event again_and_again{ Public classMessage:eventargs {Private string_word; Public stringWord {Get{return_word;} Set{_word =value;} } }}3, Eventsnamespacedelegate and event again_and_again{//define a delegate Public Delegate voidWeituohandler (Objecta,message msg); Public Partial classForm1:form {Jiafang x; PublicForm1 () {InitializeComponent (); X=NewJiafang (); X.name="Party"; Label1. Text=X.name; X.events+ = x_events;//x-click events } voidX_events (ObjectA, message msg) {TextBox1.Text="who are you ?"; TextBox1.Text+="\ t"+Msg. Word; } Private voidButton1_Click (Objectsender, EventArgs e)//saywords Button{x.saywords ();//trigger function, function to raise event } }}
Delegates and events