</pre> In a delegate, a unicast delegate can only invoke one method, there is another method in the delegate, the method can be implemented to invoke more than one method, called a multicast delegate, the way is "+ =", the implementation of calling multiple methods, you can also use "-=" Remove the fixed method. Following the previous article, we will implement multi-form communication. <p></p><p> main form </p><pre name= "code" class= "CSharp" >namespace morecontact{//< Summary>///delegate definition//</summary> public delegate void Morecontactdelegate (string word); Public partial class Frmmain:form {//Declaration delegate Morecontactdelegate Message; Public Frmmain () {InitializeComponent (); Form instantiation FrmOther1 f1 = new FrmOther1 (); FrmOther2 F2 = new FrmOther2 (); FrmOther3 F3 = new FrmOther3 (); F1. Show (); F2. Show (); F3. Show (); Call a delegate variable to associate a method with a Message = F1. Receive; Message + = F2. Receive; Message + = F3. Receive; } private string Word; Implement a fixed method by clicking Invoke Delegate private void Button1_Click (object sender, EventArgs e) { Word = TextBox1.Text; Message (word); private void Button2_Click (object sender, EventArgs e) {word = ""; Message (word); TextBox1.Text = ""; } }}
From the form 1-3 program are all the same:
<pre name= "code" class= "CSharp" >namespace morecontact{public partial class Frmother1:form { Public FrmOther1 () { InitializeComponent (); } Define method public void Receive (string word) { TextBox1.Text = Word;}} }
Implementation results:
Multicast delegation in C # implements multi-form communication