Delegate application ① -- Method/control call between windows; delegate window control call

Source: Internet
Author: User

Delegate application ① -- Method/control call between windows; delegate window control call

  

Reprinted please note address: http://www.cnblogs.com/havedream/p/4602974.html

 

I recently picked up my previous books and found that I have never had a good understanding of delegation and events, or I am not sure what the purpose of this thing is, today, we encountered a problem of calling methods between windows (the call of controls is actually the same, and the method is the same). The problem is described as follows:

In Form1, there is a Refreash () method. Form1 enables Form2 in dialog mode. After Form2 is changed, I need to refresh Form1 when Form2 is disabled, that is, call the Refreash () method of Form1.

This problem is not too difficult for me. Generally, my solution is like this: Pass Form1 directly to Form2 as a parameter, instantiate Form1 when Form2 is disabled, and call the Refreash method, method 2: Define the Refreash method as static directly. Of course, the problem arises. If your Refreash has a control, you need to change the private of the control to public so that no error will be reported!

I want to implement it by delegation. In addition, I think it is easier to understand the delegation as a thigh-holding mode. I wrote my understanding in the comments and read it carefully, here are my solutions:

 

Create a Form2 window with the following code:

1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. linq; 7 using System. text; 8 using System. threading. tasks; 9 using System. windows. forms; 10 11 namespace Learn_Form_Delegate12 {13 public partial class Form2: form14 {15 /// <summary> 16 /// define delegate 17 /// </summary> 18 public delegate void testDelegate (); 19 /// <summary> 20 // define the delegate event 21 /// </summary> 22 public event testDelegate refreshForm; 23 public Form2 () 24 {25 InitializeComponent (); 26} 27 28 private void button#click (object sender, EventArgs e) 29 {30 // execute the delegate event 31 // What is the specific execution? form2 is not concerned, 32 // If you subscribe to my refreshForm event, 33 // when executing refreshForm, you must respond to 34 refreshForm (); 35 this. close (); 36} 37} 38}


The following code is Form1:

1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. linq; 7 using System. text; 8 using System. threading. tasks; 9 using System. windows. forms; 10 11 namespace Learn_Form_Delegate12 {13 public partial class Form1: Form14 {15 public Form1 () 16 {17 InitializeComponent (); 18} 19 20 private void button#click (object s Ender, EventArgs e) 21 {22 Form2 f = new Form2 (); 23 // subscribe to the refreshForm () event of Form2 here. How do you execute it, 24 // I bound my RefreshForm1 () to your refreshForm () event, 25 // no matter when and wherever you have executed your refreshForm () event, 26 // then my refreshForm1 () event will be executed, because I have hugged your thigh! 27 f. refreshForm + = new Form2.testDelegate (RefreshForm1); 28 f. ShowDialog (); 29} 30 31 public void RefreshForm1 () 32 {33 button1.Text = "Good job! "; 34} 35} 36}

The execution result is as follows:

I didn't quite understand the observer mode before. After writing this article, I think it can be understood in a more general way. It is called the thigh mode. I hold your thigh, and I will go wherever you go, as long as you execute, I am on your thigh, and I have to execute it too! I don't know. Do you think my understanding is correct?

If you need source code please send an email request lipeng_3g@hotmail.com

(My opinion in this article is only for my personal understanding. If you make a mistake, you are welcome to make a picture !)

 

Related Article

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.