Multicast delegation in C # implements multi-form communication

Source: Internet
Author: User

</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

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.