Delegate application ①--between Windows Method/control call

Source: Internet
Author: User

  

Reprint Please specify address: http://www.cnblogs.com/havedream/p/4602974.html

Recently re-picked up the previous book, found that there is no good understanding of the delegate and the event, or understand that it does not know what the use of this thing, today happened to encounter a window between the method call problem (as for the control call is actually the same, the same way), the problem is described as follows:

Form1 There is a Refresh method Refreash (), Form1 Open Form2 with Dialog way, I need to change Form2, that is, when the Form2 is closed, Form1 is called Form1 () method

This problem is not too difficult for me, usually I deal with this way: The FORM1 as a parameter directly to the FORM2, and then Form2 close when the Form1 instantiation, call Refreash method, and method two, The Refreash method is directly defined as static, of course, the problem with it, if you have controls in the Refreash, you need to change the control private to public to not error!

I would like to do this in the form of a delegate, and I think it is easier to understand the delegate understanding of the thigh pattern, I have written my understanding in the comments, look closely, the following is my solution:

Create a new Form2 window with the following code:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;Ten  One namespacelearn_form_delegate A { -      Public Partial classForm2:form -     { the         /// <summary> -         ///Defining Delegates -         /// </summary> -          Public Delegate voidtestdelegate (); +         /// <summary> -         ///Defining Delegate Events +         /// </summary> A          Public Eventtestdelegate Refreshform; at          PublicForm2 () -         { - InitializeComponent (); -         } -  -         Private voidButton1_Click (Objectsender, EventArgs e) in         { -             //Execute Delegate Event to             //What exactly is executed, Form2 doesn't care, +             //If you subscribe to my Refreshform event, -             //then when I execute refreshform, you have to respond. the refreshform (); *              This. Close (); $         }Panax Notoginseng     } -}


Here is Form1, the code is as follows:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;Ten  One namespacelearn_form_delegate A { -      Public Partial classForm1:form -     { the          PublicForm1 () -         { - InitializeComponent (); -         } +  -         Private voidButton1_Click (Objectsender, EventArgs e) +         { AForm2 f =NewForm2 (); at             //subscribe here to Form2 's Refreshform () event, specifically how you execute me regardless, -             //I tied my RefreshForm1 () to your Refreshform () event, -             //no matter when and where you carry out your Refreshform () event, -             //then my RefreshForm1 () event will follow, because I'm holding your thigh!  -F.refreshform + =Newform2.testdelegate (REFRESHFORM1); - F.showdialog (); in         } -  to          Public voidRefreshForm1 () +         { -Button1. Text ="Good job! "; the         } *     } $}

The following results are performed:

I did not understand the observer pattern before, I think I can use a more common understanding, called the thigh mode, I hold your thigh, where you go I will go, as long as you carry out, I am on your lap, I have to do! Do not know that people think my understanding is right?

If you need source code please email [email protected]

(The article view is only personal understanding, if the wrong welcome to shoot Bricks!) )

Delegate application ①--between Windows Method/control call

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.