When the Winform mode form is closed, the value is assigned to open its own form (C # event Delegate edition)

Source: Internet
Author: User

In daily development, you often need to open a modal form, select an item in the modal form, and assign the ID or Name to the form that opens the modal form ......

In the Windows client, it is very simple to implement such a function, and the code will be very beautiful. Of course, this has something to do with personal writing.

There are two elegant methods, one is to use the interface to write the observer mode, and the other is to use the event Delegate. Now that you are playing on the Windows platform, use a more flexible event delegation method.

Let's look at the implementation code. (Red bold is the main important code)

Main Code of the modal form:

Public partial class SelectForm: Form
{
Public event Action <string> ReturnValue;

//..........

Private void button#click (object sender, EventArgs e)
{
If (ReturnValue! = Null) ReturnValue (this. textBox1.Text );
This. Close ();
}
}

Main Code for opening the form of the modal form:

Public partial class Form1: Form

{

//..........

Void button2_Click (object sender, EventArgs e)
{
SelectForm f = new SelectForm ();
F. ReturnValue + = (value) =>
{
This. textBox2.Text = value;
};
F. ShowDialog (this );
}

//..........

}

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.