I N T R O D U C T I O N T O D E L E G A T E S Ron Kessler 10/29/2014 Last updated 11/1/2014. Originally created in C# on 5/29/07 //--this just adds two blank lines in the message we display to make it look better static String^ CrLf = "\n\n"; //escape sequence of NewLine //--declare our delegate. We use this to actually run each function. Think of Tom running errands for me! delegate void myHelper(String^ s); #pragma region My Print Functions //---***********now create two functions to print messages************* private: void PrintHello (String^ myName) { lblMsg->Text += CrLf + "Hello there, " + myName + ". This message was created in the PrintHello Method."; } private: void PrintGoodbye(String^ myName) { lblMsg->Text += CrLf + "Goodbye, " + myName + ". This message was created in the PrintGoodbye Method."; } //*********************************************************** #pragma endregion #pragma region Handle Buttons private: System::Void btnHello_Click(System::Object^ sender, System::EventArgs^ e) { //---look at the next line: We instantiate a new delegate and pass 2 arguments just like event handlers!!! // 'this' points to the class implementing the method and '&Form1::PrintHello' sends it the memory location of that method! myHelper^ myDel = gcnew myHelper(this, &Form1::PrintHello); //the delegate runs PrintHello myDel("Ron"); } private: System::Void btnGoodbye_Click(System::Object^ sender, System::EventArgs^ e) { myHelper^ myDel = gcnew myHelper(this, &Form1::PrintGoodbye); //the delegate runs PrintGoodbye myDel("Ron"); } private: System::Void btnMulticast_Click(System::Object^ sender, System::EventArgs^ e) { //---now lets have the delegate run both methods. We create a chain of targets for it to execute //---First, run PrintHello, then PrintGoodBye and use Charlie as the name. myHelper^ myDel = gcnew myHelper(this, &Form1::PrintHello); myDel += gcnew myHelper(this, &Form1::PrintGoodbye); myDel("Charlie"); }