C # several methods for communication between forms,

Source: Internet
Author: User

C # several methods for communication between forms,

During application development, data communication between multiple forms is often required. Write several examples to summarize several common communication methods:

 

 

The main form Form1 is a ListBox. When you click to select a column, two controls in the form Form2 and Form2 are displayed, one is TextBox, showing the text of the selected column and the other is a button, when you click the button, the modified value is returned, and the text of the corresponding column is modified in Form1, and Form2 is disabled.

 

Method 1: Pass the value

First, the Form2 constructor receives a string type parameter, that is, the Text of the row selected in Form1, and sets the Text of the TextBox Control of Form2 to this string, that is, the Form1 value is passed to Form2. When the Form2 AcceptChange button is pressed, you need to modify the value of the corresponding column in The ListBox in Form1. Therefore, you can also set the ListBox control in Form1 when the parameter is also passed to Form2, all modifications are completed in Form2. The Form2 code is as follows:
  1. Publicpartial class Form2: Form
  2. {
  3. Private string text;
  4. Private ListBox lb;
  5. Private int index;
  6. // The constructor receives three parameters: select the row text, ListBox control, and select the row index.
  7. Public Form2 (string text, ListBox lb, int index)
  8. {
  9. This. text = text;
  10. This. lb = lb;
  11. This. index = index;
  12. InitializeComponent ();
  13. This. textBox1.Text = text;
  14. }
  15. Private void btnChange_Click (object sender, EventArgs e)
  16. {
  17. String text = this. textBox1.Text;
  18. This. lb. Items. RemoveAt (index );
  19. This. lb. Items. Insert (index, text );
  20. This. Close ();
  21. }
  22. }

In Form1, new Form 2 is written as follows:

  1. Public partial class Form1: Form
  2. {
  3. Int index = 0;
  4. String text = null;
  5. Public Form1 ()
  6. {
  7. InitializeComponent ();
  8. }
  9. Private void listBox1_SelectedIndexChanged (object sender, EventArgse)
  10. {
  11. If (this. listBox1.SelectedItem! = Null)
  12. {
  13. Text = this. listBox1.SelectedItem. ToString ();
  14. Index = this. listBox1.SelectedIndex;
  15. // Construct Form2 and pass parameters at the same time
  16. Form2 form2 = new Form2 (text, listBox1, index );
  17. Form2.ShowDialog ();
  18. }
  19. }

 

 

OK. The solution to method 1 is as follows. The advantage is that it is intuitive and the disadvantage is obvious. If Form 1 needs to modify one hundred controls, do we still need to input 100 parameters during the construction? Besides, if Form2 still needs to be played on other forms, Form2 will be discarded and can only be used in Form 1. Unless the overloaded constructor is written, it is not conducive to code reuse. continue to look at the next method.

Method 2: I have tried this method many times. inheritance can indeed be done, but it is not convenient. Therefore, I personally think that if inheritance is used to operate data on each other, it is not suitable, but since it is a method, let's take a look. The actual effect is ≈ 0. Form2:
  1. // Declare that Form2 inherits from Form1
  2. Public partial classForm2: Form1
  3. {
  4. Publicint index;
  5. Public ListBox lb;
  6. Public Form2 (string text)
  7. {
  8. // Set the inherited listBox to invisible
  9. This. listBox1.Visible = false;
  10. InitializeComponent ();
  11. This. textBox1.Text = text;
  12. }
  13. Private void btnChange_Click (object sender, EventArgs e)
  14. {
  15. String text = this. textBox1.Text;
  16. This. lb. Items. RemoveAt (index );
  17. This. lb. Items. Insert (index, text );
  18. This. Close ();
  19. }
  20. }
Form1:
  1. Public partial class Form1: Form
  2. {
  3. Public int index = 0;
  4. Public string text = null;
  5. Public Form1 ()
  6. {
  7. InitializeComponent ();
  8. }
  9. Private void listBox1_SelectedIndexChanged (object sender, EventArgse)
  10. {
  11. If (this. listBox1.SelectedItem! = Null)
  12. {
  13. Text = this. listBox1.SelectedItem. ToString ();
  14. Index = this. listBox1.SelectedIndex;
  15. Form2 form2 = new Form2 (text );
  16. // After Form2 is constructed, assign values to each parameter in Form2
  17. Form2.lb = this. listBox1;
  18. Form2.index = index;
  19. Form2.Show ();
  20. }
  21. }
  22. }
Which Assignment Method is required for each attribute in Form2? From Java, we all know that using the keyword super in the subclass of Java can access common methods and parameters in the base class, while in C #, super is replaced by base, does that mean we can assign a value to the parameter in Form2?
  1. This. lb = base. listBox1;
  2. This. index = base. index;
OK. There is no problem with the second method. You can save the index value, but for the ListBox control, this assignment will cause problems. Through testing, I found that base. listBox1 points to the listBox1 object inherited by the subclass, and is not the base class's own listBox1 object. So we guess, is the base. index value also pointing to the index of the subclass? The test shows that this is true, so this. index = base. if the index is not written, it can still be used. Because the index is inherited by Form2, we can understand that the form in C # inherits from base. the widget cannot operate the basic controls. Method 3: Event Callback since C # has an event, why not? Moreover, events play a more convenient role in form communication. We know that events are actually state capturing, at the end, I will give an example of the capture status. First, let's look at the examples of data operations. Form2:
  1. // Define a delegate that requires a string-type parameter
  2. Publicdelegate void MyDelegate (string text );
  3. Public partial class Form2: Form1
  4. {
  5. // Define the delegate event
  6. Public event MyDelegate MyEvent;
  7. Public Form2 (string text)
  8. {
  9. InitializeComponent ();
  10. This. textBox1.Text = text;
  11. }
  12. Private void btnChange_Click (object sender, EventArgs e)
  13. {
  14. // Trigger the event and send the modified text back
  15. MyEvent (this. textBox1.Text );
  16. This. Close ();
  17. }
  18. }
Form1:
  1. Public partial class Form1: Form
  2. {
  3. Public int index = 0;
  4. Public string text = null;
  5. Public Form1 ()
  6. {
  7. InitializeComponent ();
  8. }
  9. Private void listBox1_SelectedIndexChanged (object sender, EventArgse)
  10. {
  11. If (this. listBox1.SelectedItem! = Null)
  12. {
  13. Text = this. listBox1.SelectedItem. ToString ();
  14. Index = this. listBox1.SelectedIndex;
  15. Form2 form2 = new Form2 (text );
  16. // Register the MyEvent event of the form2_MyEvent Method
  17. Form2.MyEvent + = new MyDelegate (form2_MyEvent );
  18. Form2.Show ();
  19. }
  20. }
  21. // Process
  22. Void form2_MyEvent (string text)
  23. {
  24. This. listBox1.Items. RemoveAt (index );
  25. This. listBox1.Items. Insert (index, text );
  26. }
  27. }
It can be seen that it is very convenient to use events, and there is no need to pass so many parameters, there is no inheritance relationship, and code reuse is improved. Therefore, under general requirements, we recommend that you use this method.

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.