C # delegated instance (cross-form operation control)

Source: Internet
Author: User

 

Today I have studied that in C #, cross-form call controls can be implemented without complicated methods such as custom messages. A better way for C # is to delegate.

Effect Description: there are two forms: FORM1 (A button control named "Open form2") and FORM2 (A button control named "Change form1 color ). At startup, click the button control "Open form2" in FORM1 to display FORM2. Click "Change form1 color" in FORM2 to change the color of Form1.

1. In Form2:

First declare a delegate and delegate instance

Out-of-class Form2

 

Public delegate void ChangeFormColor (bool topmost );

In Form2 class

 

Public event ChangeFormColor ChangeColor;

Call the delegate in the button event of Form2

 

Private void button#click (object sender, EventArgs e)

{

ChangeColor (true); // executes the delegated instance

}

 

Ii. In Form1:

The click event of the button control "Open form2" contains the following code:

 

{

Form2 f = new Form2 ();

F. ChangeColor + = new ChangeFormColor (f_ChangeColor );

F. Show ();

}

F. ChangeColor + = new ChangeFormColor (f_ChangeColor );

This sentence is the most critical. After you enter + =, press the Tab twice and it will automatically generate a callback function for you, as shown below:

 

Void f_ChangeColor (bool topmost)

{

This. BackColor = Color. LightBlue;

This. Text = "changed successfully ";

}

 

 

Iii. complete code

 

Using System;

Using System. Drawing;

Using System. Windows. Forms;

 

Namespace cross-form Call Control

{

Public partial class Form1: Form

{

Public Form1 ()

{

InitializeComponent ();

}

Private void button#click (object sender, EventArgs e)

{

Form2 f = new Form2 ();

F. ChangeColor + = new ChangeFormColor (f_ChangeColor );

F. Show ();

}

Void f_ChangeColor (bool topmost)

{

This. BackColor = Color. LightBlue;

This. Text = "changed successfully ";

}

}

}

 

 

 

Using System;

Using System. Windows. Forms;

 

Namespace cross-form Call Control

{

Public delegate void ChangeFormColor (bool topmost );

Public partial class Form2: Form

{

Public Form2 ()

{

InitializeComponent ();

}

Public event ChangeFormColor ChangeColor;

Private void button#click (object sender, EventArgs e)

{

ChangeColor (true); // executes the delegated instance

}

}

}

 

Finally, we will introduce you to the simplest C # Cross-form operation.

 


From tu jiankai's column

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.