Use a delegate and event to transmit parameters between two forms.

Source: Internet
Author: User

Use a delegate and event to transmit parameters between two forms.
Passing parameters between two forms using delegation and event

Reference: http://blog.sina.com.cn/s/blog_49e3ed640102w0wm.html

In program development, we often transmit parameters between different forms. How can we make the transfer more convenient and effective? In fact, it is a good choice to use delegation and events for this job. A simple example is provided. The general idea is to create a class for passing parameters, define a delegate with parameters in the class, declare a delegate event, and define a method with parameters to trigger the event. A parameter-based method of the class is called in Form A to trigger the event, pass the parameters in Form A to the delegate event, and then pass the parameters to the method in Form B by the delegate event, these methods write the passed parameters to Form B. The specific method is as follows: 1. Define a class for passing parameters public static class chuancan {public delegate void delegateycyhande (string str1, string str2); // define a delegate public static event delegateycyhande chuanevent; // declare an event public static void Dodelegateycyhande (string str1, string str2) // define the method for triggering an event {chuanevent (str1, str2 );}} 2. The parent Form program public partial class mf: Form {public mf () {InitializeComponent (); chuancan. chuanevent + = new chuancan. dele Gateycyhande (this. DoMethod); // It is important to associate events in the constructor.} Public void DoMethod (string getstr1, string getstr2) // The listbox Assignment Method {string str = getstr1; str + = ""; str + = getstr2; listBox1.Items. add (str);} private void bt_zairucf_Click (object sender, EventArgs e) // open the subform {cf cf1 = new cf (); cf1.Show ();} private void button#click (object sender, EventArgs e) {listBox1.Items. clear () ;}} 3. subform program public partial class cf: Form {public cf () {InitializeComp Onent ();} private void bt_zairu_Click_1 (object sender, EventArgs e) {// call the Dodelegateycyhande method in the chuancan class to stimulate the event chuancan. dodelegateycyhande (this. textBox1.Text, this. textBox2.Text); textBox1.Text = ""; textBox2.Text = ""; textBox1.Focus, enter information in the child form and click the button to pass the information to the display control of the parent form. Of course, in this example, two parameters are passed, and we can increase the number of parameters as needed. In addition, I found an interesting thing while practicing this programming. That is, I don't want to trigger the event through buttons, but want to trigger the delegate event by changing the attribute value of the text box in the sub-form, that is, the result is unexpected: I enter a character in the textbox of the child form, such as "I". A character "I" is immediately displayed in The listbox of the parent form. This is normal, I then input a "" in the textbox. The "" is not displayed in the listbox of the parent form. Instead, "" is displayed in the second line, and "" is displayed in the first line. then I enter "yes", and the third line of the parent form listbox appears "we are"..., in this case, each time I add a word to the textbox, a new line is added to the listbox and all information about the textbox is displayed. If I reduce the number of characters each time, listbox will have one less word and display all the existing characters, which is interesting. Why? It may be related to the mechanism of triggering the delegated event!

 

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.