C # detailed Delegation

Source: Internet
Author: User

Beginner. net students, we often have this feeling that we all know it is useful for a technology or method, but we don't know where it can be used, therefore, the technology has been learned, but it cannot be used, so it cannot be further explored. After a long period of time, it is easy to forget. As a result, I have learned this and I understand it, but I will not use it...

For the first blog post in this blog Park, start with C # delegation.

Some time ago, I made a LAN communication tool project, in which I encountered a problem in the module that involved modifying my personal data, when you modify the profile picture in the form of a document, you can update the profile picture in the main form. Here, I think of several methods: first, multithreading; second, the Timer of C # is used to refresh the user's profile picture at a certain time and determine whether the two methods are feasible. However, it is a little tricky to use the thread to do this, in addition, it also involves checks on form calls between threads, and it is not wise to use the timer, which is always dependent on controls. What's more, we have a better way, right, that is, entrusting.

When learning C #, we all know that a delegate is a variable and you can assign the method name to it as a value. In this way, we can use this delegate variable to call this method. When I was learning, I believe everyone has a feeling: Why do I have to make a Commission and make a detour when I call a method clearly?

In fact, in many cases, we cannot directly call a method. This method may be private but must be called externally, you can use a delegate to call the API. The following is an example.

 

 


 

In this program, click Select image in Form2 to open a dialog box, select the corresponding image, and then display it on Form1. Here, we define a method in Form1 to modify the background image of the form by passing in parameters. This method is private and cannot be accessed in Form2, you can define a delegate and delegate variable in Form1, and assign the method name to it. After the image is selected in Form2, You can execute the call to this delegate to execute this method, complete the modification of the Form1 form background with the following code:
 

Public partial class Form1: Form
{
 
/// <Summary>
/// Define a delegate for displaying images
/// </Summary>
/// <Param name = "img"> </param>
Public delegate void ShowPicture (Image img );
 
/// <Summary>
/// Define the delegate type variable www.2cto.com
/// </Summary>
Public static ShowPicture OnPicSaved;
 
Public Form1 ()
{
InitializeComponent ();
 
OnPicSaved = ShowIt; // assign a value to the variable
}
 
/// <Summary>
/// Method for modifying the background color of a form
/// </Summary>
/// <Param name = "img"> </param>
Private void ShowIt (Image img)
{
This. BackgroundImage = img;
}
}
The code in Form2 is as follows:
?
Public Form2 ()
{
InitializeComponent ();
Form1 frm1 = new Form1 ();
Frm1.Show (); // display the Form1 form
}
 
 
Private void btnSelImg_Click (object sender, EventArgs e)
{
If (null = openFileDialog1.ShowDialog () // open the dialog box
{
Return;
}
 
// Display the PictureBox image in the form
PicBoxShow. Image = Image. FromFile (openFileDialog1.FileName );
 
// Call the delegate in Form1 to change the background color of Form1
Form1.OnPicSaved (picBoxShow. Image );
}
}
In my understanding, there are many ways to implement a function. Each method represents an idea and an idea. For this example, we can use a thread or a timer, let Form1 constantly judge whether Form2 has selected a new image. If so, I will change it. With the use of delegation, the initiative was handed over to Form2, and Form2 chose an image to tell me that I would change it again. There is no best method, but only the most appropriate method. For example, when a LAN uses Socket Communication and the synchronous blocking mode, a thread is required to process sessions with other users. In this case, the thread is selected instead of the delegate.
In fact, you can write simple things in the blog Park with great pleasure. You don't have to write a highly advanced technical article. If you live in others' eyes, you will never find yourself.

 


From white light

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.